Why my code cannot pass the oj?


  • 0
    L
    public static String convert(String s, int nRows) {
        if(s.length()==0||s.length()==1 || nRows==1 || s.length()<=nRows){
            return s;
        }
    	int lastNum = s.length()%(nRows-1);
    	int rowNum = s.length()/(nRows-1);
    	if(lastNum>0){
    		rowNum+=1;
    	}
    	int colNum = nRows-1;
        char[][] convertCharArr = new char[rowNum][colNum];
        int rowCnt = 0;
        for(int i=0;i<s.length();i++){
        	int colCnt = i%colNum;
        	convertCharArr[rowCnt][colCnt] = s.charAt(i);
        	if(colCnt==colNum-1){
        		rowCnt++;
        	}
        }
        String outStr = "";
        for(int i=0;i<convertCharArr.length;i+=2){
        	outStr+=convertCharArr[i][0];
        }
        
        for(int j=1;j<colNum;j++){
        	for(int i=0;i<convertCharArr.length;i++){
        		if(i%2==0){
    				outStr+=convertCharArr[i][j];
            	}else if(i==convertCharArr.length-1 && j==lastNum-1){
            		continue;
            	}else{
            		outStr+=convertCharArr[i][convertCharArr[i].length-j];
            	}
        	}
        }
        for(int i=1;i<convertCharArr.length;i+=2){
        	outStr+=convertCharArr[i][0];
        }
        return outStr;
    }
    

    In fact, the answer is correct and no efficiency complaint.


Log in to reply
 

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