Just a simple easy-to-thought c++ algorithm


  • 0
    J
    class Solution {
    public:
        string convert(string s, int numRows) {
            if(numRows == 1)
                return s;
            string ret = "";
            for(int i = 0; i < numRows; ++i) {
                int first = (numRows - i - 1) * 2, second = i * 2;
                int index = i, flag = 0;
                while(index < s.length()) {
                    ret += s[index];
                    if(first == 0 || second == 0)
                        index += (numRows - 1) * 2;
                    else if(flag % 2 == 0){
                        flag++;
                        index += first;
                    } else {
                        flag++;
                        index += second;
                    }
                }
            }
            return ret;
        }
    };
    

    find each gap in each step


Log in to reply
 

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