DI String Match

给一个string, 求一个数组, 如果string的i位是’I’则递增, 如果是’D’则递减. 直接写就好.

class Solution {
    public int[] diStringMatch(String S) {
        int min = 0;
        int max = S.length();
        int[] res = new int[S.length() + 1];
        for(int i = 0 ; i < S.length(); i++) {
            if(S.charAt(i) == 'I') {
                res[i] = min++;
            }
            else if(S.charAt(i) == 'D') {
                res[i] = max--;
            }
        }
        if(S.charAt(S.length() - 1) == 'I'){
            res[S.length()] = res[S.length() - 1]+1; 
            
        }else{
            res[S.length()] = res[S.length() - 1]-1; 
        }
        return res;
    }
}