C++ simple solution in 72ms and 9 lines


  • 11
    vector<int> findPermutation(string s) {
      vector<int> ret;
      for (int i = 0; i <= s.size(); ++i)
        if (i == s.size() || s[i] == 'I')
          for (int j = i + 1, lenTmp = ret.size(); j > lenTmp; --j)
            ret.push_back(j);
      return ret;
    }
    
    

  • 0
    V

    Simplifying this code even further... we do not need to count "D"-s, it's just a difference between current pointer and result array's size.

    vector<int> findPermutation(string s) {
        vector<int> ret;
        for (int i = 0; i <= s.size(); ++i) {
          if (i == s.size() || s[i] == 'I') {
            int retLen = ret.size();
            for (int countD = i - retLen + 1; countD > 0; --countD) ret.push_back(retLen + countD);
          }
        }
        return ret;
    }   
    

  • 0

    @votrubac I totally agree with you and I have removed the variable "countD"


  • 1

    I have to say that your solution is elegant and concise. Impressed by your thought.


Log in to reply
 

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