Some problem happened,I can't find why.Someone Help. thanks!


  • 0
    S
    public String convert(String s, int numRows){
    	if (s.length() <= numRows || numRows <= 1)
    		return s;
    	
    	String result = "";
    	StringBuilder sb = new StringBuilder();
    	int maxskip = 2*numRows - 2;
    	for(int i = 0; i < s.length(); i += maxskip) {
    		sb.append(s.charAt(i));
    	}
    	result += sb.toString();
    	
    	sb.setLength(0);
    	int skip1 = 2*numRows - 4;
    	int skip2 = 2;
    	int curskip = skip1;
    	for(int i = 1; i < numRows -1; i++){			
    		sb.append(s.charAt(i));
    		for (int j = i; j < s.length(); ){
    			if (curskip == skip1){
    				j += curskip;
    				if (j < s.length()) {
    					sb.append(s.charAt(j));
    					curskip = skip2;
    				}
    			} 
    			if (curskip == skip2){
    				j += curskip;
    				if (j < s.length()) {
    					sb.append(s.charAt(j));
    					curskip = skip1;
    				}
    			} 
    		}
    		result += sb.toString();
    		sb.setLength(0);
    		if(skip1 >= 2) {
    			skip1 -= 2;
    			skip2 += 2;
    		}
    	}
    	
    	sb.setLength(0);
    	for (int i = numRows -1; i < s.length(); i += maxskip){
    		sb.append(s.charAt(i));
    	}
    	result += sb.toString();
    	
    	return result;
    }

Log in to reply
 

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