欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

算法 6:模拟运算 - 之字形变化

最编程 2024-10-12 14:48:50
...

在这里插入图片描述

class Solution {
public:
    string convert(string s, int numRows) {
        int n = s.size();
        if(n <= numRows || numRows == 1) return s;
        int d = 2 * numRows - 2;
        string res;
        for(int j = 0; j < n; j += d)
        {
            res += s[j];    
        }
        for(int i = 1; i < numRows - 1; ++i)
        {   int left = d - 2 * i;
            int righ = d - left;
            bool flag = true;
            for(int j = i; j < n;)
            {
                res += s[j];
                if(flag)    j += left;
                else j+= righ;
                flag = !flag;
            }
        }
        for(int j = numRows - 1; j < n; j += d)
        {
            res += s[j];    
        }
        return res;
    }
};

上一篇: 使用 github

下一篇: 【作业题】

推荐阅读