43
43
44
44
<!-- 这里可写通用的实现逻辑 -->
45
45
46
- ** 方法一:数组或哈希表 + 贪心**
46
+ ** 方法一:贪心**
47
47
48
- 我们先用数组或哈希表 ` last ` 记录字符串 $s$ 中每个字母最后一次出现的位置。
48
+ 我们先用数组或哈希表 $ last$ 记录字符串 $s$ 中每个字母最后一次出现的位置。
49
49
50
- 接下来使用贪心的方法 ,将字符串划分为尽可能多的片段:
50
+ 接下来我们使用贪心的方法 ,将字符串划分为尽可能多的片段。
51
51
52
- 从左到右遍历字符串,遍历的同时维护当前片段的开始下标 $left $ 和结束下标 $right $,初始均为 $0$。
52
+ 从左到右遍历字符串 $s$ ,遍历的同时维护当前片段的开始下标 $j $ 和结束下标 $i $,初始均为 $0$。
53
53
54
- 对于每个访问到的字母 $c$,获取到最后一次出现的位置 $last[ c] $。由于当前片段的结束下标一定不会小于 $last[ c] $,因此令 $right = \max(right , last[ c] )$。
54
+ 对于每个访问到的字母 $c$,获取到最后一次出现的位置 $last[ c] $。由于当前片段的结束下标一定不会小于 $last[ c] $,因此令 $mx = \max(mx , last[ c] )$。
55
55
56
- 当访问到下标 $right $ 时,意味着当前片段访问结束,当前片段的下标范围是 $[ left ,.. right ] $,长度为 $right - left + 1$,我们将其添加到结果数组中。然后令 $left = right + 1$, 继续寻找下一个片段。
56
+ 当访问到下标 $mx $ 时,意味着当前片段访问结束,当前片段的下标范围是 $[ j ,.. i ] $,长度为 $i - j + 1$,我们将其添加到结果数组中。然后令 $j = i + 1$, 继续寻找下一个片段。
57
57
58
58
重复上述过程,直至字符串遍历结束,即可得到所有片段的长度。
59
59
69
69
class Solution :
70
70
def partitionLabels (self , s : str ) -> List[int ]:
71
71
last = {c: i for i, c in enumerate (s)}
72
+ mx = j = 0
72
73
ans = []
73
- left = right = 0
74
74
for i, c in enumerate (s):
75
- right = max (right , last[c])
76
- if i == right :
77
- ans.append(right - left + 1 )
78
- left = right + 1
75
+ mx = max (mx , last[c])
76
+ if mx == i :
77
+ ans.append(i - j + 1 )
78
+ j = i + 1
79
79
return ans
80
80
```
81
81
@@ -92,11 +92,12 @@ class Solution {
92
92
last[s. charAt(i) - ' a' ] = i;
93
93
}
94
94
List<Integer > ans = new ArrayList<> ();
95
- for (int i = 0 , left = 0 , right = 0 ; i < n; ++ i) {
96
- right = Math . max(right, last[s. charAt(i) - ' a' ]);
97
- if (i == right) {
98
- ans. add(right - left + 1 );
99
- left = right + 1 ;
95
+ int mx = 0 , j = 0 ;
96
+ for (int i = 0 ; i < n; ++ i) {
97
+ mx = Math . max(mx, last[s. charAt(i) - ' a' ]);
98
+ if (mx == i) {
99
+ ans. add(i - j + 1 );
100
+ j = i + 1 ;
100
101
}
101
102
}
102
103
return ans;
@@ -112,13 +113,16 @@ public:
112
113
vector<int > partitionLabels(string s) {
113
114
int last[ 26] = {0};
114
115
int n = s.size();
115
- for (int i = 0; i < n; ++i) last[ s[ i] - 'a'] = i;
116
+ for (int i = 0; i < n; ++i) {
117
+ last[ s[ i] - 'a'] = i;
118
+ }
116
119
vector<int > ans;
117
- for (int i = 0, left = 0, right = 0; i < n; ++i) {
118
- right = max(right, last[ s[ i] - 'a'] );
119
- if (i == right) {
120
- ans.push_back(right - left + 1);
121
- left = right + 1;
120
+ int mx = 0, j = 0;
121
+ for (int i = 0; i < n; ++i) {
122
+ mx = max(mx, last[ s[ i] - 'a'] );
123
+ if (mx == i) {
124
+ ans.push_back(i - j + 1);
125
+ j = i + 1;
122
126
}
123
127
}
124
128
return ans;
@@ -129,21 +133,20 @@ public:
129
133
### **Go**
130
134
131
135
```go
132
- func partitionLabels(s string) []int {
133
- last := make([]int, 26)
134
- n := len(s)
135
- for i := 0; i < n; i++ {
136
- last[s[i]-'a'] = i
136
+ func partitionLabels(s string) (ans []int) {
137
+ last := [26]int{}
138
+ for i, c := range s {
139
+ last[c-'a'] = i
137
140
}
138
- var ans [] int
139
- for i, left, right := 0, 0, 0; i < n; i++ {
140
- right = max(right , last[s[i] -'a'])
141
- if i == right {
142
- ans = append(ans, right-left +1)
143
- left = right + 1
141
+ var mx, j int
142
+ for i, c := range s {
143
+ mx = max(mx , last[c -'a'])
144
+ if mx == i {
145
+ ans = append(ans, i-j +1)
146
+ j = i + 1
144
147
}
145
148
}
146
- return ans
149
+ return
147
150
}
148
151
149
152
func max(a, b int) int {
@@ -158,19 +161,18 @@ func max(a, b int) int {
158
161
159
162
``` ts
160
163
function partitionLabels(s : string ): number [] {
164
+ const last: number [] = Array (26 ).fill (0 );
165
+ const idx = (c : string ) => c .charCodeAt (0 ) - ' a' .charCodeAt (0 );
161
166
const n = s .length ;
162
- let last = new Array (26 );
163
- for (let i = 0 ; i < n ; i ++ ) {
164
- last [s .charCodeAt (i ) - ' a' .charCodeAt (0 )] = i ;
167
+ for (let i = 0 ; i < n ; ++ i ) {
168
+ last [idx (s [i ])] = i ;
165
169
}
166
- let ans = [];
167
- let left = 0 ,
168
- right = 0 ;
169
- for (let i = 0 ; i < n ; i ++ ) {
170
- right = Math .max (right , last [s .charCodeAt (i ) - ' a' .charCodeAt (0 )]);
171
- if (i == right ) {
172
- ans .push (right - left + 1 );
173
- left = right + 1 ;
170
+ const ans: number [] = [];
171
+ for (let i = 0 , j = 0 , mx = 0 ; i < n ; ++ i ) {
172
+ mx = Math .max (mx , last [idx (s [i ])]);
173
+ if (mx === i ) {
174
+ ans .push (i - j + 1 );
175
+ j = i + 1 ;
174
176
}
175
177
}
176
178
return ans ;
@@ -184,21 +186,21 @@ impl Solution {
184
186
pub fn partition_labels (s : String ) -> Vec <i32 > {
185
187
let n = s . len ();
186
188
let bytes = s . as_bytes ();
187
- let mut inx_arr = [0 ; 26 ];
189
+ let mut last = [0 ; 26 ];
188
190
for i in 0 .. n {
189
- inx_arr [(bytes [i ] - b 'a' ) as usize ] = i ;
191
+ last [(bytes [i ] - b 'a' ) as usize ] = i ;
190
192
}
191
- let mut res = vec! [];
192
- let mut left = 0 ;
193
- let mut right = 0 ;
193
+ let mut ans = vec! [];
194
+ let mut j = 0 ;
195
+ let mut mx = 0 ;
194
196
for i in 0 .. n {
195
- right = right . max (inx_arr [(bytes [i ] - b 'a' ) as usize ]);
196
- if right == i {
197
- res . push ((right - left + 1 ) as i32 );
198
- left = i + 1 ;
197
+ mx = mx . max (last [(bytes [i ] - b 'a' ) as usize ]);
198
+ if mx == i {
199
+ ans . push ((i - j + 1 ) as i32 );
200
+ j = i + 1 ;
199
201
}
200
202
}
201
- res
203
+ ans
202
204
}
203
205
}
204
206
```
@@ -211,25 +213,47 @@ impl Solution {
211
213
* @return {number[]}
212
214
*/
213
215
var partitionLabels = function (s ) {
216
+ const last = new Array (26 ).fill (0 );
217
+ const idx = c => c .charCodeAt () - ' a' .charCodeAt ();
214
218
const n = s .length ;
215
- let last = new Array (26 );
216
- for (let i = 0 ; i < n; i++ ) {
217
- last[s .charCodeAt (i) - ' a' .charCodeAt (0 )] = i;
219
+ for (let i = 0 ; i < n; ++ i) {
220
+ last[idx (s[i])] = i;
218
221
}
219
- let ans = [];
220
- let left = 0 ,
221
- right = 0 ;
222
- for (let i = 0 ; i < n; i++ ) {
223
- right = Math .max (right, last[s .charCodeAt (i) - ' a' .charCodeAt (0 )]);
224
- if (i == right) {
225
- ans .push (right - left + 1 );
226
- left = right + 1 ;
222
+ const ans = [];
223
+ for (let i = 0 , j = 0 , mx = 0 ; i < n; ++ i) {
224
+ mx = Math .max (mx, last[idx (s[i])]);
225
+ if (mx === i) {
226
+ ans .push (i - j + 1 );
227
+ j = i + 1 ;
227
228
}
228
229
}
229
230
return ans;
230
231
};
231
232
```
232
233
234
+ ### ** C#**
235
+
236
+ ``` cs
237
+ public class Solution {
238
+ public IList <int > PartitionLabels (string s ) {
239
+ int [] last = new int [26 ];
240
+ int n = s .Length ;
241
+ for (int i = 0 ; i < n ; i ++ ) {
242
+ last [s [i ] - 'a' ] = i ;
243
+ }
244
+ IList < int > ans = new List <int >();
245
+ for (int i = 0 , j = 0 , mx = 0 ; i < n ; ++ i ) {
246
+ mx = Math .Max (mx , last [s [i ] - 'a' ]);
247
+ if (mx == i ) {
248
+ ans .Add (i - j + 1 );
249
+ j = i + 1 ;
250
+ }
251
+ }
252
+ return ans ;
253
+ }
254
+ }
255
+ ```
256
+
233
257
### ** ...**
234
258
235
259
```
0 commit comments