C++ straightforward solution with iterator and insert()


  • 2
    H
    class Solution {
    public:
        vector<int> findPermutation(string s) {
            vector<int> res(1, 1);
            auto b = res.begin();
            int i = 2;
            for (auto c : s) {
                if (c == 'D') {
                    b = res.insert(b, i++);
                }
                else {
                    res.push_back(i++);
                    b = prev(res.end());
                }
            }
            return res;
        }
    };
    

Log in to reply
 

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