1 Public classSolution {2 3 Public Static voidMain (string[] args) {4String s = "paypalishiring";5String res = CONVERT (S, 4);6 System.out.println (res);7 }8 9 /**Ten * Numrows=1 and numrows=2 for special cases One */ A Public StaticString Convert (String s,intnumrows) { -String res = ""; - intL =s.length (); the if(L = = 0) { - return""; - } - + if(L > 0 && L <=numrows) { - returns; + } A at if(NumRows = = 1) { - returns; - } - - //Col is the number of columns - intcol = L/(2 * numRows-2); in intremainder = l% (2 * numRows-2); - if(Remainder >= 0 && remainder <=numrows) { toCol = 2 * col + 1; + } - if(Remainder >numrows) { theCol = 2 * col + 2; * } $ Panax Notoginseng //temp is a secondary array - int[] temp =New int[col]; theTemp[0] = 1; + for(inti = 1; I < col; i++) { ATemp[i] = 2 * I * (numRows-1)-temp[i-1]; the } + for(inti = 0; i < numrows; i++) { - if(i = = 0) { $ intj = 0; $ while(2 * J * (NUMROWS-1) <l) { -Res + = S.charat (2 * j * (numRows-1)); -J + +; the } - Continue;Wuyi } the if(i = = NumRows-1) { - intj = 0; Wu while((2 * j + 1) * (NUMROWS-1) <l) { -Res + = S.charat ((2 * j + 1) * (numRows-1)); AboutJ + +; $ } - Continue; - } - for(intk = 0; K < Col; k++) { A if(k = = 0 && i <l) { +Res + =S.charat (i); the Continue; - } $ the if(k%2==0){ the if(temp[k]+i-1<l) { theRes + = S.charat (temp[k]+i-1); the Continue; - } in } the the if(k% 2 = = 1) { About if(Temp[k]-i + 1 <l) { theRes + = S.charat (Temp[k]-i + 1); the Continue; the } + } - Break; the }Bayi the } the - returnRes; - } the}
Ideas:
Leetcode OJ S_06