[C++] Clean Code - 9 lines - reverse delta


  • 1
    class Solution {
    public:
        string convert(string s, int n) {
            if (s.length() < n || n == 1) return s;
            string res;
            vector<string> rows(n, string());
    
            for (int i = 0, r = 0, d = 1; i < s.length(); i++, r += d) { // r: rowid, d: delta
                if (r == 0)     d = 1;
                if (r == n - 1) d = -1;
                rows[r] += s[i];
            }
    
            for (auto row : rows) res += row;
    
            return res;
        }
    };
    

Log in to reply
 

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