C# solution, beats 97%


  • 0
    H

    public class Solution {

    public string Convert(string s, int numRows) {
        if(numRows == 1) return s;
        
        int N = s.Length;  // Use local variable for efficiency
        
        StringBuilder sb = new StringBuilder();
        
        int gap =  2 *(numRows -1);    //Maximum gap width at the top or bottom
        
        for( int i = 0; i < numRows; i++){
            int curr = i;
    
            int offset = gap - (2 * i);   //Each middle layer is increase or decrease by 2
        
            while( curr  < N){
                if( offset != 0){
                    sb.Append( s[curr]);
                    curr += offset;
                }
                
               offset = gap - offset;
            }
        }
        
        return sb.ToString();
    }
    

    }


Log in to reply
 

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