Easy C# solution


  • 0
    P
    public class Solution {
        public string Convert(string s, int numRows) {
            if (s.Length <= 2 || numRows == 1)
                return s;
                
            StringBuilder sb = new StringBuilder();
            int space=numRows*2-2;
            
            for (int i=numRows; i>=1; i--){
                int gap= i*2-2, start=numRows-i;
                
                while (start < s.Length){
                    sb.Append(s[start]); // down
                    if((gap != space && gap != 0)&& (start+gap)<s.Length)
                       sb.Append(s[start+gap]); //up
                       
                    start= start+ space;
                }
            }
            
            return sb.ToString();
        }
    }
    

Log in to reply
 

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