forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_484.java
46 lines (42 loc) · 1.37 KB
/
_484.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.fishercoder.solutions;
public class _484 {
public static class Solution1 {
/**
* credit:https://discuss.leetcode.com/topic/76221/java-o-n-clean-solution-easy-to-understand
* <p>
* For example, given IDIIDD we start with sorted sequence 1234567
* Then for each k continuous D starting at index i we need to reverse [i, i+k] portion of the sorted sequence.
* <p>
* e.g.
* IDIIDD
* <p>
* 1234567 // sorted
* 1324765 // answer
*/
public int[] findPermutation(String s) {
int[] result = new int[s.length() + 1];
for (int i = 0; i <= s.length(); i++) {
result[i] = i + 1;
}
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'D') {
int left = i;
while (i < s.length() && s.charAt(i) == 'D') {
i++;
}
reverse(result, left, i);
}
}
return result;
}
private void reverse(int[] result, int left, int i) {
while (left < i) {
int temp = result[left];
result[left] = result[i];
result[i] = temp;
left++;
i--;
}
}
}
}