C++ two pointers solution


  • 0
    G
    class Solution {
    public:
    
        vector<int> findPermutation(string s) 
        {
            vector<int> Result(s.size() + 1,0);
            
            for(int i = 1; i <= s.size() + 1; ++i)
            {
                Result[i - 1] = i;
            }
            
            int start = 0;
            int second = 0;
            
            while(second <= s.size())
            {
                if(second == s.size() || s[second] == 'I')
                {
                    reverse(Result.begin() + start, Result.begin() + second + 1);
                    
                    if(second == s.size())
                    {
                        break;
                    }
                    
                    second++;
                    start = second;
                }
                else
                {
                    second++;
                }
            }
            
            return (Result);
        }
    };

Log in to reply
 

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