[LeetCode][JavaScript]ZigZag Conversion

ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R

And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);

convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

https://leetcode.com/problems/zigzag-conversion/


号称easy,我怎么感觉并不是很水...

colDirect标记是走竖的还是走斜的,根据情况调整下标。

该死的js还没有二维数组。

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

[LeetCode][JavaScript]ZigZag Conversion的相关文章

leetcode -day16 ZigZag Conversion

1.  ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line b

leetCode 6. ZigZag Conversion 字符串 (上传费劲)

6. ZigZag Conversion 题目:https://leetcode.com/problems/zigzag-conversion/ string convert2(string s, int numRows) { if (s.length() < 2 || numRows < 2) return s; int cycle = 2 * numRows - 2; string tmp; string result; for (int i = 0; i < numRows; i+

【LeetCode】ZigZag Conversion

ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line

leetCode 6. ZigZag Conversion 字符串

6. ZigZag Conversion

LeetCode 6 - ZigZag Conversion

原题如下: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHN

LeetCode 6 ZigZag Conversion 模拟 难度:0

https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y

LeetCode 6 ZigZag Conversion(规律)

题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I

[LeetCode][Python]ZigZag Conversion

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this:(you may want to display this pattern in

[LeetCode]7. ZigZag Conversion ZigZag转换

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H NA P L S I I GY I R And then read line by line: "PAHNAPLSIIGY