Simpe accepted java code


  • 5
    F
        public class Solution {
        public String convert(String s, int numRows) {
            String []lines=new String[numRows];
    		for (int i=0;i<numRows;i++){
    			lines[i]="";
    		}
    		char []a=s.toCharArray();
    		int dir=-1;
    		int row=0;
    		for (int i=0;i<a.length;i++){
    			lines[row]+=a[i];
    			if(row==0 || row==numRows-1)
    				dir*=-1;
    			row+=dir;
    			row%=numRows;
    		}
    			
    		String res="";
    		for (String line : lines)
    			res+=line;
    		return res;
    	
        }
    }

  • 0
    I

    I have the same thoughts, but you have simplified code...


  • 0
    Z

    Good thoughts! but the row%=numRows can be deleted if you add a simple return s if numRows==1 at front


Log in to reply
 

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