【Leetcode】【Easy】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".

解题:

引用http://blog.csdn.net/zhouworld16/article/details/14121477的图片说明:

所谓的ZigZag,就是这样的:

因此可以找到每行排列数字的规律,nRows = n,字符串字符下标从0开始。

普通青年:

①   先读第1行:0, 0 + 2n-2, 0 + (2n-2)*2,...

②   读第2行至第n-2行,设行数为e,则每行读取数字为:

  e-1,e-1 + 2n-2 - (e-1)*2, e-1 + 2n-2, e-1+2n-2 + 2n-2 - (e-1)*2, ... 

  规律即,两个数字一组,每组间隔2n-2:

  for (int i=e-1; i<string.length(); i+=2n-2) {

    每组元素可表示为i 和 i + 2n-2 - (e-1)*2

  }

③  读最后一行:n-1, n-1 + 2n-2, n-1 + (2n-2)*2, ...

(由于每循环一组,会读出两个数字。注意判定字符串越界)

 1 class Solution {
 2 public:
 3     string convert(string s, int nRows) {
 4         string res;
 5         int len = s.length();
 6         
 7         if (nRows == 0)
 8             return res;
 9             
10         if (nRows == 1)
11             return s;
12             
13         for(int i=0; i<len; i+=2*nRows-2)
14             res.push_back(s[i]);
15         
16         int index = 1;
17         while(nRows-1-index){
18             for(int i=index; i<len; i+=2*nRows-2) {
19                 res.push_back(s[i]);
20                 int zig = i + 2 * nRows - 2 * index - 2;
21                 if (zig < len)
22                     res.push_back(s[zig]);
23             }
24             index++;
25         }
26         
27         for(int i=nRows-1; i<len; i+=2*nRows-2)
28             res.push_back(s[i]);
29         
30         return res;
31 
32     }
33 };

聪明的青年:

类似于期末考试考场排座位,一般为蛇形排号。将考场的座位按一竖列为一组,编号时,第一组的座位从前向后编号,对第二组从后向前进行编号,如下图:

① ⑧ ⑨

② ⑦       ...

③ ⑥

④ ⑤

nRows即为每组能坐多少人。因此先让同学们按编号入座,再从前向后按横排依次读出他们的编号即为本题答案。

 1 class Solution {
 2 public:
 3     string convert(string s, int nRows) {
 4         string sArray[nRows];
 5         string res = "";
 6         int index = 0;
 7         
 8         if (nRows == 1)
 9             return s;
10         
11         for(int i=0; i<s.length(); ++i){
12             sArray[index] += s[i];
13             
14             if (!(i / (nRows-1) % 2)) {
15                 index++;
16             } else {
17                 index--;
18             }
19         }
20 
21         for(int i=0; i<nRows; ++i){
22             res += sArray[i];
23         }
24 
25         return res;
26     }
27 };

当然是原创的。

原文地址:https://www.cnblogs.com/huxiao-tee/p/4195833.html