public String convert(String s, int nRows) {
    	
    	int len = s.length();
    	
    	if ((nRows==1)||(len<nRows)) return s;
        
        int block = nRows*2-2;
        int nBlock = (len+block-len%block)/block;
        int[][] location = new int[nRows][nBlock*(nRows-1)];
        
        // reseting the location of each char
        main:
        for (int i = 0; i < nBlock; i++) {
        	for (int j = 0; j < block; j++) {
        		int num = i*block+j;
        		if (num >= len) break main;
				if (j < nRows){
					location[j][i*(nRows-1)] = num;
				}
				else{
					location[2*(nRows-1)-j][i*(nRows-1)+j-nRows+1] = num;
				}
			}
		}
        
        // output string
        StringBuilder builder = new StringBuilder();
        builder.append(s.charAt(0));
        for (int i = 0; i < nRows; i++) {
        	for (int j = 0; j < nBlock*(nRows-1); j++) {
        		int locOld = location[i][j];
        		if (locOld!=0){
        			builder.append(s.charAt(locOld));
        		}
			}
		}
        return builder.toString();
    }