java solution , bests 100%


  • -1
    public class Solution {
        public int[] findPermutation(String s) {
            char[] trends = s.toCharArray();
            int [] ret = new int[trends.length+1];
            for(int i=0;i<ret.length;i++) ret[i]=i+1;
            for(int i=0;i<trends.length;i++)
            {
                int pos=i;
                while(pos<trends.length&&trends[pos]=='D') pos++;
                reverse(ret,i,pos);
                i=pos;
            }
            return ret;
            
        }
        
        public void reverse(int[] ret,int s, int e)
        {
         
          for(int i=s;i<=s+(e-s)/2;i++)
          {
            int tmp= ret[i];
            ret[i]=ret[e-(i-s)];
            ret[e-(i-s)]=tmp;
          }
        }
    }
    

Log in to reply
 

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