|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | /**
|
4 |
| - 484. Find Permutation |
| 4 | + * 484. Find Permutation |
| 5 | + * |
5 | 6 | * By now, you are given a secret signature consisting of character 'D' and 'I'.
|
6 | 7 | * 'D' represents a decreasing relationship between two numbers, 'I' represents an increasing relationship between two numbers.
|
7 | 8 | * 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 | 27 | The length of input string is a positive integer and will not exceed 10,000
|
27 | 28 | */
|
28 | 29 | public class _484 {
|
| 30 | + public static class Solution1 { |
29 | 31 |
|
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); |
51 | 56 | }
|
52 |
| - reverse(result, left, i); |
53 | 57 | }
|
| 58 | + return result; |
54 | 59 | }
|
55 |
| - return result; |
56 |
| - } |
57 | 60 |
|
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 | + } |
65 | 69 | }
|
66 | 70 | }
|
67 | 71 |
|
|
0 commit comments