码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode ---6.ZigZag Conversion

时间:2017-05-15 22:24:55      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:tin   etc   blog   ++   span   str   nbsp   div   logs   

 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

标签:tin   etc   blog   ++   span   str   nbsp   div   logs   

原文地址:http://www.cnblogs.com/zousanchuan/p/6858459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!