LeetCode-6 Z字形变换

  • C++ 
class Solution {
public:
    string convert(string s, int numRows) {
        if(numRows==1){return s;}
        int length = s.length();
        int nums = (numRows<<1) - 2;
        string coverted;
        int index = 0;
        while(index<length){
            coverted.push_back(s[index]);
            index += nums;
        }
        for(int i=0;i<numRows-2;i++){
            index = i + 1;
            while(index<length){
                coverted.push_back(s[index]);
                index += (numRows - 2 - i) * 2;
                if(index<length){
                    coverted.push_back(s[index]);
                    index += (i + 1) * 2;
                }
            }
        }
        index = numRows - 1;
        while(index<length){
            coverted.push_back(s[index]);
            index += nums;
        }
        return coverted;
    }
};

猜你喜欢

转载自blog.csdn.net/lolimostlovely/article/details/82945901