Clean Java solution


  • 0
    Y
        public int[] findPermutation(String s) {
            int n = s.length() + 1;
            int[] result = new int[n];
            for(int i = 0; i < n; i ++) {
                result[i] = i + 1;
            }
            int start = 0, end = -1;
            while(end < n-1) {
                while(start <= end || (start < n - 1 && s.charAt(start) != 'D')) start ++;
                while(end <= start || (end < n - 1 && s.charAt(end) == 'D')) end ++;
                if(start == n - 1) break;
                reverse(result, start, end);
            }
            return result;
        }
        
        public void reverse(int[] result, int i, int j) {
            while(i < j) {
                int temp = result[i];
                result[i ++] = result[j];
                result[j --] = temp;
            }
        }
    

Log in to reply
 

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