A short C# O(n) time O(n) space solution


  • 3
    M
    public class Solution {
        public string Convert(string s, int numRows) {
            if (numRows==1)
                return s;
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < numRows; i++){
                int increment = i * 2;
                for(int j=i; j<s.Length; j+=increment!=0?increment:numRows*2-2){
                    sb.Append(s[j]);
                    increment = numRows*2-2 - increment;
                }
            }
            return sb.ToString();
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.