ZigZag Conversion leetcode

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".

Subscribe to see which companies asked this question

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

我们可以将其划分为4个区域

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

每个区域的数字个数计算公式为:

cycle = (2 * nRows - 2)

该例中为 2*nRows - 2 = 4.

每个区域中, 除了第一行和最后一行,每行都有2个元素

假设行号是i,则每行的第一个元素下标j为

j = i + cycle*k, k = 0, 1, 2, ...

第二个元素下标为:

secondJ = (j - i) + cycle - i

(j-i) 是当前区域的第一个, (j-i) + cycle 是下个区域的第一个

代码如下:

string convert(string s, int nRows) {
    if (nRows <= 1) return s;
    string result = "";
    //the size of a cycle(period)
    int cycle = 2 * nRows - 2;
    for (int i = 0; i < nRows; ++i)
    {
        for (int j = i; j < s.length(); j = j + cycle) {
            result = result + s[j];
            int secondJ = (j - i) + cycle - i;
            if (i != 0 && i != nRows - 1 && secondJ < s.length())
                result = result + s[secondJ];
        }
    }
    return result;
}
时间: 2024-08-04 22:06:50

ZigZag Conversion leetcode的相关文章

ZigZag Conversion leetcode java

题目: 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: "PAHNAP

LeetCode ZigZag Conversion(将字符串排成z字型)

1 class Solution { 2 public: 3 string convert(string s, int nRows) { 4 string a=""; 5 int len=s.length(); 6 if(len<=nRows||nRows==1) return s; //只有n个,不足一个周期||排成一行 7 int teams=len/(nRows*2-2); //teams个完整的周期 8 int rest=len%(nRows*2-2); //最后一个不足

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】006 ZigZag Conversion

题目:LeetCode 006 ZigZag Conversion 题意:给一个字符串"PAYPALISHIRING" 按照ZigZag的形式,即按照下面的顺序排列,然后 在转换成一行一行的读取方式,得到"PAHNAPLSIIGYIR".其中行数不定. 思路:肯定是不能去开一个二维数组的,必须要找出规律来直接输出.发现间隔是和总行数numRows有关,另外,第一行和最后一行需要单独考虑,每两个字符间隔一致,为2*(numRows-1):其余行有两种间隔,设当前为第L

LeetCode.ZigZag Conversion ,Container With Most Water

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 lin

【leetcode刷题笔记】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:  "PAHNAPLSI

LeetCode——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: "PAHNAPLSII

[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