My simple java solution


  • 4
    H
    public class Solution {
    public String convert( String s, int nRows ){
        if( nRows == 1 )    return s;
    	StringBuilder[] res = new StringBuilder[nRows];
    	for( int i=0; i<nRows; i++ )
    		res[i] = new StringBuilder();
    	int loop = 2 * nRows-2;
    	int mid = nRows-1;
    	for( int i=0; i<s.length(); i++ )
    		res[ mid - Math.abs( i % loop - mid ) ].append(s.charAt(i));
    	for( int i = 1; i<nRows; i++ )
    		res[0].append( res[i] );
    	return res[0].toString();
    }
    

    }


  • 0
    A

    genioussssssssssssssssssssssssssssss


Log in to reply
 

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