首页 > 代码库 > LeetCode ---6.ZigZag Conversion
LeetCode ---6.ZigZag Conversion
1 public String convert(String s, int numRows) { 2 if(numRows == 1) return s; 3 StringBuilder str = new StringBuilder(""); 4 char[] ch = s.toCharArray(); 5 int block_m = 0; 6 int block_n = 0; 7 int i = 0, j = 0; 8 char[][] store = new char[numRows][(numRows - 1) * (ch.length / (2 * numRows - 2) + 1)]; 9 for(int index = 0; index < ch.length; index ++) { 10 block_m = index / (2 * numRows - 2); 11 block_n = index % (2 * numRows - 2); 12 if(block_n / numRows == 0) { 13 i = block_n % numRows; 14 j = block_m * (numRows - 1); 15 } else { 16 i = numRows - block_n % numRows -2; 17 j = block_m * (numRows - 1) + block_n % numRows +1; 18 } 19 char a = ch[index]; 20 store[i][j] = a; 21 } 22 for(i = 0; i < numRows; i ++) { 23 for(j = 0; j < store[0].length; j ++) { 24 if(store[i][j] == ‘\u0000‘) 25 continue; 26 str = str.append(store[i][j]); 27 28 } 29 } 30 return str.toString(); 31 }
LeetCode ---6.ZigZag Conversion
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。