Solution by msadique


  • 0
    M

    '''class Solution {
    public String convert(String s, int numRows) {
    char[][] row = new char[numRows][s.length()];
    int i,j,turn=0;

        for(i = 0; i < numRows; i++ )
            for( j=0; j < s.length(); j++ )
                row[ i ][ j ] = ' ';
        i = 0;
        j = 0;
        
        if( numRows == 1 )
            return s;
        for(int k=0; k < s.length(); k++ )
        {
            if( turn == 0 )
                row[ i++ ][ j ] = s.charAt( k );
            else            //turn 1
                row[ i-- ][ ++j ]=s.charAt( k );
            if( i == numRows )
            {
                i = i - 2; 
                turn=1;
            }
            else if( i == -1 ){
                i = i + 2;
                turn = 0;
            }
        }
        String str = "";  //append in string 
        for(i = 0; i < numRows; i++ )
            for( j = 0; j < s.length(); j++ )
                if( row[i][j] != ' ' ){
                    str = str + row[i][j];
                }
        return str;
    }
    

    }'''


Log in to reply
 

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