|
3 | 3 | import java.util.ArrayDeque;
|
4 | 4 | import java.util.Queue;
|
5 | 5 |
|
6 |
| -/** |
7 |
| - * 942. DI String Match |
8 |
| - * |
9 |
| - * Given a string S that only contains "I" (increase) or "D" (decrease), let N = S.length. |
10 |
| - * |
11 |
| - * Return any permutation A of [0, 1, ..., N] such that for all i = 0, ..., N-1: |
12 |
| - * |
13 |
| - * If S[i] == "I", then A[i] < A[i+1] |
14 |
| - * If S[i] == "D", then A[i] > A[i+1] |
15 |
| - * |
16 |
| - * Example 1: |
17 |
| - * Input: "IDID" |
18 |
| - * Output: [0,4,1,3,2] |
19 |
| - * |
20 |
| - * Example 2: |
21 |
| - * Input: "III" |
22 |
| - * Output: [0,1,2,3] |
23 |
| - * |
24 |
| - * Example 3: |
25 |
| - * Input: "DDI" |
26 |
| - * Output: [3,2,0,1] |
27 |
| - * |
28 |
| - * Note: |
29 |
| - * 1 <= S.length <= 10000 |
30 |
| - * S only contains characters "I" or "D". |
31 |
| - */ |
32 | 6 | public class _942 {
|
33 |
| - public static class Solution1 { |
34 |
| - public int[] diStringMatch(String S) { |
35 |
| - Queue<Integer> deque = new ArrayDeque<>(); |
36 |
| - for (int i = 0; i <= S.length(); i++) { |
37 |
| - deque.add(i); |
38 |
| - } |
39 |
| - int[] result = new int[S.length() + 1]; |
40 |
| - for (int i = 0; i <= S.length(); i++) { |
41 |
| - if (i == S.length()) { |
42 |
| - result[i] = ((ArrayDeque<Integer>) deque).pollLast(); |
43 |
| - } else if (S.charAt(i) == 'I') { |
44 |
| - result[i] = ((ArrayDeque<Integer>) deque).pollFirst(); |
45 |
| - } else { |
46 |
| - result[i] = ((ArrayDeque<Integer>) deque).pollLast(); |
| 7 | + public static class Solution1 { |
| 8 | + public int[] diStringMatch(String S) { |
| 9 | + Queue<Integer> deque = new ArrayDeque<>(); |
| 10 | + for (int i = 0; i <= S.length(); i++) { |
| 11 | + deque.add(i); |
| 12 | + } |
| 13 | + int[] result = new int[S.length() + 1]; |
| 14 | + for (int i = 0; i <= S.length(); i++) { |
| 15 | + if (i == S.length()) { |
| 16 | + result[i] = ((ArrayDeque<Integer>) deque).pollLast(); |
| 17 | + } else if (S.charAt(i) == 'I') { |
| 18 | + result[i] = ((ArrayDeque<Integer>) deque).pollFirst(); |
| 19 | + } else { |
| 20 | + result[i] = ((ArrayDeque<Integer>) deque).pollLast(); |
| 21 | + } |
| 22 | + } |
| 23 | + return result; |
47 | 24 | }
|
48 |
| - } |
49 |
| - return result; |
50 | 25 | }
|
51 |
| - } |
52 | 26 | }
|
0 commit comments