DI String Match
给一个string, 求一个数组, 如果string的i位是’I’则递增, 如果是’D’则递减. 直接写就好.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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; } } |