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

Zigzag Conversion

时间:2015-11-25 10:06:21      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

 1 var convert = function(s, numRows) {
 2     if (numRows === 1) {
 3         return s;
 4     }
 5 
 6     var i, j,
 7         count = 0,
 8         colDirect = true,
 9         row = 0,
10         col = 0,
11         map = [];
12 
13     for (i = 0; i < s.length; i++) {
14         map[i] = [];
15     }
16 
17     for (i = 0; i < s.length; i++) {
18         map[row][col] = s[i];
19 
20         if (colDirect) {
21             row++;
22         } else {
23             row--;
24             col++;
25         }
26 
27         count++;
28 
29         if (colDirect && count === numRows) {
30             row -= 2;
31             col++;
32             count = 0;
33             colDirect = false;
34         }
35 
36         if (!colDirect && count === numRows - 2) {
37             count = 0;
38             colDirect = true;
39         }
40     }
41 
42     var res = "";
43     for (i = 0; i < map.length; i++) {
44         for (j = 0; j < map[i].length; j++) {
45             if (map[i][j]) {
46                 res += map[i][j];
47             }
48         }
49     }
50 
51     return res;
52 };

 

Zigzag Conversion

标签:

原文地址:http://www.cnblogs.com/huoteng/p/4993763.html

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