LeetCode 06 ZigZag Conversion

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

水题纯考细心

题目:依照Z字形来把一个字符串写成矩阵,然后逐行输出矩阵。

O(n)能够处理掉

记i为行数

第0行和第numRow-1行。 ans += str[i+k*(numRows*2-2)], k=0,1,2,...

其它, 每一个Z字形(事实上仅仅是一竖一斜两条线)须要加上两个,下标见代码。

特殊处理:numRows=1的时候numRows*2-2==0 ,会死循环的。另外numRows=1的时候直接输出即可

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

class Solution {
public:
    string convert(string s, int numRows) {
        string ret;
        if(numRows == 1){
            return s;
        }
        for(int i=0; i<numRows; i++){
            if(i == 0 || i == numRows-1){
                int j=i;
                while(j<s.size()){
                    ret = ret + s[j];
                    j += numRows*2-2;
                }
            }else{
                int j=i;
                while(j<s.size()){
                    ret = ret + s[j];
                    if(j+numRows*2-2-(i*2) < s.size())ret = ret + s[j+numRows*2-2-(i*2)];
                    j += numRows*2-2;
                }
            }

        }
        return ret;
    }
};

int main(){
    string s;
    int numRows;
    Solution sol;
    while(cin >> s >> numRows){
        cout << sol.convert(s, numRows) << endl;
    }
    return 0;
}
时间: 2024-12-15 01:55:09

LeetCode 06 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