[leetcode刷题系列]ZigZag Conversion

嗯, 模拟题- - 


class Solution {
public:
    string convert(string s, int nRows) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function    
        if(nRows == 1)
            return s;
        vector<vector<char> > data;
        data.resize(nRows);
        int nowr = 0;
        for(int i = 0; i < s.size(); ++ i){
            char c = s[i];
            data[nowr].push_back(c);
            int p = i % (nRows + nRows - 2);
            if(p < nRows - 1){
                nowr ++ ;
            }else{
                nowr --;
            }
        }
        string ans = "";
        for(int i = 0; i < data.size(); ++ i)
            for(int j = 0; j < data[i].size(); ++ j)
                ans += data[i][j];
        return ans;
    }
};


猜你喜欢

转载自blog.csdn.net/sigh1988/article/details/9992915
今日推荐