slow but pass


  • 1
    0
    class Solution {
    public:
        vector<int> findPermutation(const string& s) {
            vector<int> ret{1};
            for (int i = 0, n = s.size(); i < n; i++) {
                ret.push_back(i + 2);
                int p = i + 1;
                while (p > 0 && ((ret[p-1] < ret[p] && s[p-1] == 'D')||(ret[p-1] > ret[p] && s[p - 1] == 'I'))) {
                    swap(ret[p], ret[p -1]);
                    p--;
                }
            }
            return ret;
        }
    };

Log in to reply
 

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