C# (idea from others)


  • 0
    H
    public int[] FindPermutation(string s) {
            int n = s.Length, i = 0, v = 1;
            int[] r = new int [n + 1];
            
            for(i = 0; i <= n; i++) r[i] = i + 1;
            
            i = 0;
            while(i < n) {
                while(i < n && s[i] == 'I') i++;
                int sd = i;
                while(i < n && s[i] == 'D') i++;
                int j = sd, k = i;
                while(j < k) {
                    int t = r[j];
                    r[j++] = r[k];
                    r[k--] = t;
                }
            }
            
            return r;
        }
    

Log in to reply
 

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