[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: "PAHNAPLSIIGYIR"

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

string convert(string s, int numRows);

Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"

Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:

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

 

這道題剛開始看了半天沒看懂是咋樣變換的,上網查了些資料,終於搞懂了,就是要把字符串擺成一個之字型的,比如有一個字符串 "0123456789ABCDEF",轉為 zigzag 如下所示:

當 n = 2 時:

0 2 4 6 8 A C E

1 3 5 7 9 B D F

當 n = 3 時:

0   4    8     C

1 3 5 7 9 B D F

2    6   A     E

當 n = 4 時:

0     6        C

1   5 7   B  D

2 4   8 A    E

3      9       F

 

可以發現,除了第一行和最后一行沒有中間形成之字型的數字外,其他都有,而首位兩行中相鄰兩個元素的 index 之差跟行數是相關的,為  2*nRows - 2, 根據這個特點,可以按順序找到所有的黑色元素在元字符串的位置,將他們按順序加到新字符串里面。對於紅色元素出現的位置(Github 上可能無法正常顯示顏色,請參見博客園上的帖子)也是有規律的,每個紅色元素的位置為 j + 2 x numRows-2 - 2 x i, 其中,j為前一個黑色元素的 index,i為當前行數。 比如當 n = 4 中的那個紅色5,它的位置為 1 + 2 x 4-2 - 2 x 1 = 5,為原字符串的正確位置。知道了所有黑色元素和紅色元素位置的正確算法,就可以一次性的把它們按順序都加到新的字符串里面。代碼如下:

 

解法一:

class Solution {
public:
    string convert(string s, int numRows) {
        if (numRows <= 1) return s;
        string res;
        int size = 2 * numRows - 2, n = s.size();
        for (int i = 0; i < numRows; ++i) {
            for (int j = i; j < n; j += size) {
                res += s[j];
                int pos = j + size - 2 * i;
                if (i != 0 && i != numRows - 1 && pos < n) res += s[pos];
            }
        }
        return res;
    }
};

 

若上面解法中的規律不是很好想的話,我們也可以用下面這種更直接的方法來做,建立一個大小為 numRows 的字符串數組,為的就是把之字形的數組整個存進去,然后再把每一行的字符拼接起來,就是想要的結果了。順序就是按列進行遍歷,首先前 numRows 個字符就是按順序存在每行的第一個位置,然后就是 ‘之’ 字形的連接位置了,可以發現其實都是在行數區間 [1, numRows-2] 內,只要按順序去取字符就可以了,最后把每行都拼接起來即為所求,參見代碼如下:

 

解法二:

class Solution {
public:
    string convert(string s, int numRows) {
        if (numRows <= 1) return s;
        string res;
        int i = 0, n = s.size();
        vector<string> vec(numRows);
        while (i < n) {
            for (int pos = 0; pos < numRows && i < n; ++pos) {
                vec[pos] += s[i++];
            }
            for (int pos = numRows - 2; pos >= 1 && i < n; --pos) {
                vec[pos] += s[i++];
            }
        }
        for (auto &a : vec) res += a;
        return res;
    }
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/6

 

類似題目:

Zigzag Iterator

Binary Tree Zigzag Level Order Traversal

 

參考資料:

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

https://www.cnblogs.com/springfor/p/3889414.html

https://leetcode.com/problems/zigzag-conversion/discuss/3403/Easy-to-understand-Java-solution

https://leetcode.com/problems/zigzag-conversion/discuss/3417/A-10-lines-one-pass-o(n)-time-o(1)-space-accepted-solution-with-detailed-explantation

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM