Skip to content

Commit fc2318d

Browse files
refactor 484
1 parent 3c0b592 commit fc2318d

File tree

1 file changed

+36
-32
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+36
-32
lines changed

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

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.fishercoder.solutions;
22

33
/**
4-
484. Find Permutation
4+
* 484. Find Permutation
5+
*
56
* By now, you are given a secret signature consisting of character 'D' and 'I'.
67
* 'D' represents a decreasing relationship between two numbers, 'I' represents an increasing relationship between two numbers.
78
* And our secret signature was constructed by a special integer array, which contains uniquely all the different number from 1 to n (n is the length of the secret signature plus 1).
@@ -26,42 +27,45 @@
2627
The length of input string is a positive integer and will not exceed 10,000
2728
*/
2829
public class _484 {
30+
public static class Solution1 {
2931

30-
/**credit:https://discuss.leetcode.com/topic/76221/java-o-n-clean-solution-easy-to-understand
31-
*
32-
For example, given IDIIDD we start with sorted sequence 1234567
33-
Then for each k continuous D starting at index i we need to reverse [i, i+k] portion of the sorted sequence.
34-
35-
e.g.
36-
IDIIDD
37-
38-
1234567 // sorted
39-
1324765 // answer
40-
*/
41-
public int[] findPermutation(String s) {
42-
int[] result = new int[s.length() + 1];
43-
for (int i = 0; i <= s.length(); i++) {
44-
result[i] = i + 1;
45-
}
46-
for (int i = 0; i < s.length(); i++) {
47-
if (s.charAt(i) == 'D') {
48-
int left = i;
49-
while (i < s.length() && s.charAt(i) == 'D') {
50-
i++;
32+
/**
33+
* credit:https://discuss.leetcode.com/topic/76221/java-o-n-clean-solution-easy-to-understand
34+
*
35+
* For example, given IDIIDD we start with sorted sequence 1234567
36+
* Then for each k continuous D starting at index i we need to reverse [i, i+k] portion of the sorted sequence.
37+
*
38+
* e.g.
39+
* IDIIDD
40+
*
41+
* 1234567 // sorted
42+
* 1324765 // answer
43+
*/
44+
public int[] findPermutation(String s) {
45+
int[] result = new int[s.length() + 1];
46+
for (int i = 0; i <= s.length(); i++) {
47+
result[i] = i + 1;
48+
}
49+
for (int i = 0; i < s.length(); i++) {
50+
if (s.charAt(i) == 'D') {
51+
int left = i;
52+
while (i < s.length() && s.charAt(i) == 'D') {
53+
i++;
54+
}
55+
reverse(result, left, i);
5156
}
52-
reverse(result, left, i);
5357
}
58+
return result;
5459
}
55-
return result;
56-
}
5760

58-
private void reverse(int[] result, int left, int i) {
59-
while (left < i) {
60-
int temp = result[left];
61-
result[left] = result[i];
62-
result[i] = temp;
63-
left++;
64-
i--;
61+
private void reverse(int[] result, int left, int i) {
62+
while (left < i) {
63+
int temp = result[left];
64+
result[left] = result[i];
65+
result[i] = temp;
66+
left++;
67+
i--;
68+
}
6569
}
6670
}
6771

0 commit comments

Comments
 (0)