C++ code: Oscillational element assigning process


  • 0
    N

    class Solution {
    public:
    string convert(string s, int numRows) {
    string result="";

        if(numRows<=1||s.size()<=1)return s;
        int m=0;
        
        vector<vector<char> >store(numRows);
        int direction=-1;
        for(int i=0;i<s.size();i++){
            if(m%numRows==0||m%numRows==numRows-1)direction=-direction;
            store[m].push_back(s[i]);
            m+=direction;
        }
        
        for(int i=0;i<numRows;i++)
            for(int j=0;j<store[i].size();j++)
                result+=store[i][j];
                
       return result;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.