Z-字形变换【模拟】

题目链接

解题思路:简单模拟

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

 

posted @ 2022-01-25 18:52  夜灯长明  阅读(38)  评论(0)    收藏  举报