942. DI String Match
Input: "IDID"
Output: [0,4,1,3,2]Input: "III"
Output: [0,1,2,3]Input: "DDI"
Output: [3,2,0,1]解题要点:
class Solution {
public int[] diStringMatch(String S) {
int N = S.length();
int[] res = new int[N + 1];
if(N == 0) return res;
int start = 0, count = 0;
for(char c : S.toCharArray()){
if(c == 'I') res[count++] = start++;
if(c == 'D') res[count++] = N--;
}
res[count] = start;
return res;
}
}Last updated