Skip to content

Commit f77b4ce

Browse files
refactor 942
1 parent 54e6a78 commit f77b4ce

File tree

1 file changed

+17
-43
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+17
-43
lines changed

src/main/java/com/fishercoder/solutions/_942.java

+17-43
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,24 @@
33
import java.util.ArrayDeque;
44
import java.util.Queue;
55

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-
*/
326
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;
4724
}
48-
}
49-
return result;
5025
}
51-
}
5226
}

0 commit comments

Comments
 (0)