mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0763.划分字母区间.md
0763.划分字母区间增加C语言实现
This commit is contained in:
@ -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#
|
### C#
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
public class Solution
|
public class Solution
|
||||||
{
|
{
|
||||||
@ -435,4 +466,3 @@ public class Solution
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user