[LeetCode] Zigzag Conversion

简介: The key challenge to this problem is to make the code clean. This post has shared a nice example, which is rewritten below in C++.

The key challenge to this problem is to make the code clean. This post has shared a nice example, which is rewritten below in C++.

class Solution {
public:
    string convert(string s, int numRows) {
        vector<string> vs(numRows, "");
        int n = s.length(), i = 0;
        while (i < n) {
            for (int j = 0; j < numRows && i < n; j++)
                vs[j].push_back(s[i++]);
            for (int j = numRows - 2; j >= 1 && i < n; j--)
                vs[j].push_back(s[i++]);
        }
        string zigzag;
        for (string v : vs) zigzag += v;
        return zigzag;
    } 
};

 

目录
相关文章
|
5月前
Leetcode 6.ZigZag Conversion
如上所示,这就是26个小写字母表的5行曲折变换。 其中在做这道题的时候把不需要我们构造出这样五行字符,然后拼接。其实你把字母换成1-n的数字,很容易找到每个位置的字母在原字符串中的位置。
23 0
|
Python
LeetCode 103. BTree Zigzag Level Order Traversal
给定二叉树,返回其节点值的Z字形级别遍历。 (即,从左到右,然后从右到左进行下一级别并在之间交替)。
51 0
LeetCode 103. BTree Zigzag Level Order Traversal
|
索引
Leetcode-Medium 6. ZigZag Conversion
Leetcode-Medium 6. ZigZag Conversion
68 0
Leetcode-Medium 6. ZigZag Conversion
LeetCode---Problem6 ZigZag Conversion
ZigZag问题思路。代码整洁并不一定执行速度就好~
762 0
|
机器学习/深度学习 Perl
LeetCode - 6. ZigZag Conversion
6. ZigZag Conversion  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个字符串,让你将其按照倒‘之’字型排列,然后输出排列后的顺序.
879 0