(字符串)ZigZag Conversion

【解析】

第一次看到这个题目的人,可能不知道ZigZag是什么意思,简单解释一下,就是把字符串原顺序012345……按下图所示排列:

发现所有行的重复周期都是 2 * nRows - 2

对于首行和末行之间的行,还会额外重复一次,重复的这一次距离本周期起始字符的距离是 2 * nRows - 2 - 2 * i

[cpp] view plain copy

    1. class Solution {
    2. public:
    3. string convert(string s, int nRows) {
    4. // Start typing your C/C++ solution below
    5. // DO NOT write int main() function
    6. string result;
    7. if (nRows < 2) return s;
    8. for (int i = 0;i < nRows;++i) {
    9. for (int j = i;j < s.length();j += 2 * (nRows - 1)) {
    10. result.push_back(s[j]);
    11. if (i > 0 && i < nRows - 1) {
    12. if (j + 2 * (nRows - i - 1) < s.length())
    13. result.push_back(s[j + 2 * (nRows - i - 1)]);
    14. }
    15. }
    16. }
    17. return result;
    18. }
    19. };
时间: 2024-10-17 17:32:26

(字符串)ZigZag Conversion的相关文章

字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--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 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 6. ZigZag Conversion 字符串

6. ZigZag Conversion

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+

No.006 ZigZag Conversion

6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy 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 bette

LeetCode【6】. ZigZag Conversion --思路图解与java实现

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

【LeetCode】006 ZigZag Conversion

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

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 ,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: "PAHNAPLSII