Why it reports Time Limit Exceeded


  • 0
    D
                public String convert(String s, int nRows) {
                 int i;
    	StringBuffer result = new StringBuffer();
    	int j = 2*nRows-2; 
    	System.out.println("s.length"+s.length());
    	if(s.length() == 0 || nRows < 2)
    		return s;
    	if(s.length() < nRows){ 
    		return s;	
    	}
    	for (i=0;i<nRows;i++){
    		int f = i;
    		result.append(s.charAt(i));
    		int g = j - i;
    		System.out.println("g="+g);
    		if( (g!=i) &&  ( g<s.length() ) ) {
    			result.append(s.charAt(g));
    			while(true){
    				f+=2*nRows-2;
    				if(f<s.length() && f!=g) {
    					result.append(s.charAt(f));
    				}else {
    					break;
    				}
    				g+=2*nRows-2;
    				if(g<s.length()) {
    					result.append(s.charAt(g));
    				}else {
    					break;
    				}
    			}
    		}
    	}
    	System.out.println("result="+result);
    	return result.toString();
    

    }


Log in to reply
 

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