C++_Use vector<string>_Accepted_16ms_62.96%


  • 0
    class Solution {
    public:
    string convert(string s, int numRows) {
        int n = s.size();
        if(numRows >= n || numRows == 1) return s;
        
        vector<string> res(numRows, "");
        int row = 0, direction = 1;
        
       //just push every char into corresponding rows and use **direction** to control the direction of row changes
    
        for(int i = 0; i < n; i++){
            res[row] += s[i];
            if(row == 0){direction = 1;}
            else if(row == numRows - 1){direction = -1;}
            row += direction;
        }
        string ans = "";
        for(int i = 0; i < numRows; i++){
            ans += res[i];
        }
        return ans;
    }
    };
    

    0_1475097839137_172EE5F4-34F9-4668-B1C7-F9C83F3A0179.jpg


Log in to reply
 

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