C++ easy to understand solution


  • 0
    C

    //my solution, start with the first letter case, then if it's an I, just append it to the end of the array. Otherwise, search to the left of the existing array till no I is found, insert the number there
    vector<int> findPermutation(string s) {
    vector<int> res;
    if (s.empty())
    return res;

    if (s[0]=='D'){
        res.push_back(2);
        res.push_back(1);
    }
    else {
        res.push_back(1);
        res.push_back(2);
    }
    
    for (int i=1; i<s.size(); i++){
        if (s[i]=='I'){
            res.push_back(i+2);
        }
        else {
            int j=i;
            while(j>=0 && s[j]!='I'){
                j--;
            }
            
            res.insert(res.begin()+(j+1), i+2);
        }
    }
    
    return res;
    

    }


Log in to reply
 

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