关闭

LeetCode OJ(6.ZigZag Conversion)

标签: leetcode
201人阅读 评论(0) 收藏 举报
分类:

题目:
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”.
题目练习的意义不大,主要是找到数学规律。

class Solution {
public:
    string convert(string s, int nRows) {
        string result;
        if (nRows <=1) return s;
        for (int i = 0; i < nRows; ++i) {
            for (int j = i; j < s.length(); j += 2 * (nRows - 1)) {
                result.push_back(s[j]);
                if (i > 0 && i < nRows - 1) {
                    if (j + 2 * (nRows - i - 1) < s.length())
                        result.push_back(s[j + 2 * (nRows - i - 1)]);
                }
            }
        }
        return result;
    }
};
0
0
查看评论

LeetCode OJ 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 pa...
  • sinat_26659461
  • sinat_26659461
  • 2016-02-28 13:19
  • 142

Leetcode 6.ZigZag Conversion

原题链接:6. ZigZag Conversion   题目名称『曲折变换』,但给的样例只有三行,看不出到底怎么样曲折变化,所以这里我再补充一个样例,大家肯定一下就懂了。 a i q y b h j p r x z c ...
  • xindoo
  • xindoo
  • 2017-07-31 22:03
  • 111

【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 ...
  • KatherineLeeyq
  • KatherineLeeyq
  • 2016-04-18 02:04
  • 145

[LeetCode] 6.ZigZag Conversion

题目描述:https://leetcode.com/problems/zigzag-conversion/  ZigZag Conversion My Submissions Question  Solution  Tota...
  • huaxixidongbeishida
  • huaxixidongbeishida
  • 2015-09-26 18:59
  • 176

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 thi...
  • caishenfans
  • caishenfans
  • 2015-02-02 15:30
  • 264

LeetCode题目:6.ZigZag Conversion

LeetCode题目:6.ZigZag Conversion 原题链接:https://leetcode.com/problems/zigzag-conversion/description/ 解题思路:对指定行row,找出逻辑,得到公式,然后进行计算即可。核心思想:对第一行和第row行,一个之...
  • MyCodecOdecoDecodE
  • MyCodecOdecoDecodE
  • 2017-09-17 22:44
  • 77

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 thi...
  • a1453753484
  • a1453753484
  • 2016-07-12 15:43
  • 70

leetcode - 6.ZigZag Conversion

ZigZag Conversion
  • qq_27350929
  • qq_27350929
  • 2017-03-02 19:44
  • 50

【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 thi...
  • bao_xy
  • bao_xy
  • 2016-09-21 14:54
  • 44

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 pat...
  • wscdylzjy
  • wscdylzjy
  • 2015-03-19 20:01
  • 168
    个人资料
    • 访问:112296次
    • 积分:3383
    • 等级:
    • 排名:第11797名
    • 原创:223篇
    • 转载:15篇
    • 译文:2篇
    • 评论:19条
    最新评论