Java Simple Solution


  • 0
    S
    public class Solution {
        public int[] findPermutation(String s) {
            int[] result = new int[s.length()+1];
            for(int i = 1; i<=s.length()+1; i++) {
                result[i-1] = i;
            }
            int cur = 0;
            int dCount = 0;
            while(cur < s.length()) {
                if(s.charAt(cur) == 'I') {
                    cur++;
                    continue;
                }
                dCount = 0;
                while(cur < s.length() && s.charAt(cur) == 'D') {
                    cur++;
                    dCount++;
                }
                reverse(result, cur-dCount, dCount+1);
            }
            return result;
        }
        
        private void reverse(int[] arr, int start, int length) {
            for(int i = 0; i<length; i++) {
                arr[i+start] = start+length-i;
            }
        }
    }

Log in to reply
 

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