Easiest solution


  • 0
    D
    class Solution
    {
    	public String convert(String s, int numRows)
    	{
    		if(numRows==0) return "";
    		if(numRows==1) return s;
    		if(s.length()<=numRows) return s;
            String ans = "";
            int modulo = numRows*2-2;
            String arr[] = new String[numRows];
            for(int i=0; i<s.length(); i++)
            {
            	int val = i%modulo;
            	if(val<numRows)
            	{
            		if(arr[val]==null) arr[val] = new String();
            		arr[val]=arr[val]+s.charAt(i)+"";
            	}
            	else
            	{
            		if(arr[(numRows-1)-(val-numRows+1)]==null) arr[(numRows-1)-(val-numRows+1)] = new String();
            		arr[(numRows-1)-(val-numRows+1)]=arr[(numRows-1)-(val-numRows+1)]+s.charAt(i)+""; 
            	}
            }
            for(int i =0; i<arr.length; i++)
            {
            	ans = ans + arr[i];
            }
            return ans;
        }
    }
    

Log in to reply
 

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