parent
dd242cfbb5
commit
3c4632315c
|
|
@ -404,7 +404,38 @@ impl Solution {
|
|||
}
|
||||
}
|
||||
```
|
||||
### C
|
||||
|
||||
```c
|
||||
#define max(a, b) ((a) > (b) ? (a) : (b))
|
||||
|
||||
int* partitionLabels(char* s, int* returnSize) {
|
||||
// 记录每个字符最远出现的位置
|
||||
int last[26] = {0};
|
||||
int len = strlen(s);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
last[s[i] - 'a'] = i;
|
||||
}
|
||||
int left = 0, right = 0;
|
||||
int * partition = malloc(sizeof (int ) * len);
|
||||
// 初始化值
|
||||
*returnSize = 0;
|
||||
for(int i = 0; i < len; i++){
|
||||
right = max(right, last[s[i] - 'a']);
|
||||
// 到达最远位置,加入答案,并且更新左边下标
|
||||
if(i == right){
|
||||
partition[(*returnSize)++] = right - left + 1;
|
||||
left = i + 1;
|
||||
}
|
||||
}
|
||||
return partition;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### C#
|
||||
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
|
|
@ -435,4 +466,3 @@ public class Solution
|
|||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue