Short and readable 20ms solution C++


  • 0
    R
    string convert(string s, int numRows) {
                int first = numRows * 2 - 2, second = 0;
                string fin = "";
                int i = 0, flag, tmpi;
                if(numRows == 1)
                    return s;
                while(i < numRows) {
                    tmpi = i;
                    if(first)
                        flag = 0;
                    else
                        flag = 1;
                    while(tmpi < s.size()) {
                        fin += s[tmpi];
                        if(!flag) {
                            tmpi += first;
                            if(second != 0)
                            flag = 1 - flag;
                        }
                        else {
                            tmpi += second;
                            if(first != 0)
                            flag = 1 - flag;
                        }
                    }
                    i++;
                    first -= 2;
                    second += 2;
                }
                return fin;
        }

Log in to reply
 

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