My java code with only 6ms


  • 0
    W
    public String convert(String s, int numRows) {
        char[] zag = new char[s.length()];
        int maxd = 2*(numRows-1);
        int index = 0;
        if(numRows == 1){
            return s;
        }
        for(int i=0;i<numRows;i++){
            int d1 = 2*(numRows-1-i);
            int d2 = maxd-d1;
            int temp =i;
            if(index < s.length())
            {
                zag[index++] = s.charAt(temp);
            }
            while(temp<s.length()){
                if(d1>0){
                	if(temp+d1<s.length()){
                		temp = temp+d1;
                        zag[index++] = s.charAt(temp);
                	}
                	else{
                		break;
                	}
                    
                }
                if(d2>0){
                	if(temp+d2<s.length()){
                		temp = temp+d2;
                        zag[index++] = s.charAt(temp);
                	}
                	else{
                		break;
                	}
                }
            }
        }
        return String.valueOf(zag);
        
    }

Log in to reply
 

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