|
46 | 46 |
|
47 | 47 | <!-- 这里可写通用的实现逻辑 -->
|
48 | 48 |
|
| 49 | +**方法一:贪心** |
| 50 | + |
| 51 | +先初始化结果数组 `ans` 为 `[1, 2, 3, ..., n+1]`。 |
| 52 | + |
| 53 | +假定某个连续 `D` 子数组区间为 `[i, j)`,那么只要翻转 `ans[i: j + 1]` 即可。 |
| 54 | + |
| 55 | +因此,遍历字符串 `s`,找出所有的连续 `D` 子数组区间,将其翻转。 |
| 56 | + |
| 57 | +时间复杂度 $O(n)$,其中 $n$ 表示字符串 `s` 的长度。 |
| 58 | + |
49 | 59 | <!-- tabs:start -->
|
50 | 60 |
|
51 | 61 | ### **Python3**
|
52 | 62 |
|
53 | 63 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
54 | 64 |
|
55 | 65 | ```python
|
56 |
| - |
| 66 | +class Solution: |
| 67 | + def findPermutation(self, s: str) -> List[int]: |
| 68 | + n = len(s) |
| 69 | + ans = list(range(1, n + 2)) |
| 70 | + i = 0 |
| 71 | + while i < n: |
| 72 | + j = i |
| 73 | + while j < n and s[j] == 'D': |
| 74 | + j += 1 |
| 75 | + ans[i: j + 1] = ans[i: j + 1][::-1] |
| 76 | + i = max(i + 1, j) |
| 77 | + return ans |
57 | 78 | ```
|
58 | 79 |
|
59 | 80 | ### **Java**
|
60 | 81 |
|
61 | 82 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
62 | 83 |
|
63 | 84 | ```java
|
| 85 | +class Solution { |
| 86 | + public int[] findPermutation(String s) { |
| 87 | + int n = s.length(); |
| 88 | + int[] ans = new int[n + 1]; |
| 89 | + for (int i = 0; i < n + 1; ++i) { |
| 90 | + ans[i] = i + 1; |
| 91 | + } |
| 92 | + int i = 0; |
| 93 | + while (i < n) { |
| 94 | + int j = i; |
| 95 | + while (j < n && s.charAt(j) == 'D') { |
| 96 | + ++j; |
| 97 | + } |
| 98 | + reverse(ans, i, j); |
| 99 | + i = Math.max(i + 1, j); |
| 100 | + } |
| 101 | + return ans; |
| 102 | + } |
| 103 | + |
| 104 | + private void reverse(int[] arr, int i, int j) { |
| 105 | + for (; i < j; ++i, --j) { |
| 106 | + int t = arr[i]; |
| 107 | + arr[i] = arr[j]; |
| 108 | + arr[j] = t; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +### **C++** |
| 115 | + |
| 116 | +```cpp |
| 117 | +class Solution { |
| 118 | +public: |
| 119 | + vector<int> findPermutation(string s) { |
| 120 | + int n = s.size(); |
| 121 | + vector<int> ans(n + 1); |
| 122 | + iota(ans.begin(), ans.end(), 1); |
| 123 | + int i = 0; |
| 124 | + while (i < n) { |
| 125 | + int j = i; |
| 126 | + while (j < n && s[j] == 'D') { |
| 127 | + ++j; |
| 128 | + } |
| 129 | + reverse(ans.begin() + i, ans.begin() + j + 1); |
| 130 | + i = max(i + 1, j); |
| 131 | + } |
| 132 | + return ans; |
| 133 | + } |
| 134 | +}; |
| 135 | +``` |
64 | 136 |
|
| 137 | +### **Go** |
| 138 | +
|
| 139 | +```go |
| 140 | +func findPermutation(s string) []int { |
| 141 | + n := len(s) |
| 142 | + ans := make([]int, n+1) |
| 143 | + for i := range ans { |
| 144 | + ans[i] = i + 1 |
| 145 | + } |
| 146 | + i := 0 |
| 147 | + for i < n { |
| 148 | + j := i |
| 149 | + for ; j < n && s[j] == 'D'; j++ { |
| 150 | + } |
| 151 | + reverse(ans, i, j) |
| 152 | + i = max(i+1, j) |
| 153 | + } |
| 154 | + return ans |
| 155 | +} |
| 156 | +
|
| 157 | +func reverse(arr []int, i, j int) { |
| 158 | + for ; i < j; i, j = i+1, j-1 { |
| 159 | + arr[i], arr[j] = arr[j], arr[i] |
| 160 | + } |
| 161 | +} |
| 162 | +
|
| 163 | +func max(a, b int) int { |
| 164 | + if a > b { |
| 165 | + return a |
| 166 | + } |
| 167 | + return b |
| 168 | +} |
65 | 169 | ```
|
66 | 170 |
|
67 | 171 | ### **...**
|
|
0 commit comments