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

二. 题目分析

这道题是就是原来的字符串的元素与锯齿化后的字符串的元素之间的关系,我们可以举个例子来说明,假设原来的字符串的每一个字符的下标为0,1,2,3,…, 12分别进行行数为3,4,5行的锯齿化。

定义将原来的字符串按照nRows行进行锯齿化,定义 Step= 2 * nRows - 2; 从上面的例子可以看出,对于第i行,有下面两种情况:

1.对于第0行和第(nRows - 1)行,每一行的元素为i, i+Step, i+2*Step,…;

2.对于其他的行来说,每一行的元素为i, Step - i, i + Step, 2*Step - i,…。

三. 实例代码

class Solution {
public:
    string convert(string s, int nRows)
    {
        const int Size = s.size();
        if ((Size <= nRows) || (nRows == 1))
        {
            return s;
        }
        const int Step = 2 * nRows - 2;
        string Result;
        for (int RowIndex = 0; RowIndex < nRows; RowIndex++)
        {
            int Index = RowIndex;
            if ((RowIndex == 0) || (RowIndex == (nRows - 1)))
            {
                while (Index < Size)
                {
                    Result.push_back(s[Index]);
                    Index = Index + Step;
                }
                continue;
            }
            int SecondIndex = Step - Index;
            while ((Index < Size) || (SecondIndex < Size))
            {
                if (Index < Size)
                {
                    Result.push_back(s[Index]);
                    Index = Index + Step;
                }
                if (SecondIndex < Size)
                {
                    Result.push_back(s[SecondIndex]);
                    SecondIndex = SecondIndex + Step;
                }
            }
        }
        return Result;
    }
};

四. 小结

这道题主要是寻找原来是字符串的元素坐标与锯齿化后的字符串的坐标关系。

时间: 2024-12-20 22:48:43

leetcode笔记: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][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

【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