Skip to content

Commit bac0787

Browse files
committed
feat: add solutions to lc problem: No.0763
No.0763.Partition Labels
1 parent 140deb4 commit bac0787

File tree

10 files changed

+292
-225
lines changed

10 files changed

+292
-225
lines changed

solution/0700-0799/0763.Partition Labels/README.md

+90-66
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46-
**方法一:数组或哈希表 + 贪心**
46+
**方法一:贪心**
4747

48-
我们先用数组或哈希表 `last` 记录字符串 $s$ 中每个字母最后一次出现的位置。
48+
我们先用数组或哈希表 $last$ 记录字符串 $s$ 中每个字母最后一次出现的位置。
4949

50-
接下来使用贪心的方法,将字符串划分为尽可能多的片段
50+
接下来我们使用贪心的方法,将字符串划分为尽可能多的片段
5151

52-
从左到右遍历字符串,遍历的同时维护当前片段的开始下标 $left$ 和结束下标 $right$,初始均为 $0$。
52+
从左到右遍历字符串 $s$,遍历的同时维护当前片段的开始下标 $j$ 和结束下标 $i$,初始均为 $0$。
5353

54-
对于每个访问到的字母 $c$,获取到最后一次出现的位置 $last[c]$。由于当前片段的结束下标一定不会小于 $last[c]$,因此令 $right = \max(right, last[c])$。
54+
对于每个访问到的字母 $c$,获取到最后一次出现的位置 $last[c]$。由于当前片段的结束下标一定不会小于 $last[c]$,因此令 $mx = \max(mx, last[c])$。
5555

56-
当访问到下标 $right$ 时,意味着当前片段访问结束,当前片段的下标范围是 $[left,.. right]$,长度为 $right - left + 1$,我们将其添加到结果数组中。然后令 $left = right + 1$, 继续寻找下一个片段。
56+
当访问到下标 $mx$ 时,意味着当前片段访问结束,当前片段的下标范围是 $[j,.. i]$,长度为 $i - j + 1$,我们将其添加到结果数组中。然后令 $j = i + 1$, 继续寻找下一个片段。
5757

5858
重复上述过程,直至字符串遍历结束,即可得到所有片段的长度。
5959

@@ -69,13 +69,13 @@
6969
class Solution:
7070
def partitionLabels(self, s: str) -> List[int]:
7171
last = {c: i for i, c in enumerate(s)}
72+
mx = j = 0
7273
ans = []
73-
left = right = 0
7474
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
7979
return ans
8080
```
8181

@@ -92,11 +92,12 @@ class Solution {
9292
last[s.charAt(i) - 'a'] = i;
9393
}
9494
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;
100101
}
101102
}
102103
return ans;
@@ -112,13 +113,16 @@ public:
112113
vector<int> partitionLabels(string s) {
113114
int last[26] = {0};
114115
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+
}
116119
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;
122126
}
123127
}
124128
return ans;
@@ -129,21 +133,20 @@ public:
129133
### **Go**
130134
131135
```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
137140
}
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
144147
}
145148
}
146-
return ans
149+
return
147150
}
148151
149152
func max(a, b int) int {
@@ -158,19 +161,18 @@ func max(a, b int) int {
158161

159162
```ts
160163
function partitionLabels(s: string): number[] {
164+
const last: number[] = Array(26).fill(0);
165+
const idx = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
161166
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;
165169
}
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;
174176
}
175177
}
176178
return ans;
@@ -184,21 +186,21 @@ impl Solution {
184186
pub fn partition_labels(s: String) -> Vec<i32> {
185187
let n = s.len();
186188
let bytes = s.as_bytes();
187-
let mut inx_arr = [0; 26];
189+
let mut last = [0; 26];
188190
for i in 0..n {
189-
inx_arr[(bytes[i] - b'a') as usize] = i;
191+
last[(bytes[i] - b'a') as usize] = i;
190192
}
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;
194196
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;
199201
}
200202
}
201-
res
203+
ans
202204
}
203205
}
204206
```
@@ -211,25 +213,47 @@ impl Solution {
211213
* @return {number[]}
212214
*/
213215
var partitionLabels = function (s) {
216+
const last = new Array(26).fill(0);
217+
const idx = c => c.charCodeAt() - 'a'.charCodeAt();
214218
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;
218221
}
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;
227228
}
228229
}
229230
return ans;
230231
};
231232
```
232233

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+
233257
### **...**
234258

235259
```

0 commit comments

Comments
 (0)