Merge pull request #2250 from KingArthur0205/master

添加 0034.在排序数组中查找元素的第一个和最后一个位置.md, 0283. 移动零 C语言解法
This commit is contained in:
程序员Carl
2023-09-10 20:20:55 +08:00
committed by GitHub
3 changed files with 82 additions and 0 deletions

View File

@ -739,6 +739,60 @@ class Solution {
} }
``` ```
### C
```c
int searchLeftBorder(int *nums, int numsSize, int target) {
int left = 0, right = numsSize - 1;
// 记录leftBorder没有被赋值的情况
int leftBorder = -1;
// 边界为[left, right]
while (left <= right) {
// 更新middle值等同于middle = (left + right) / 2
int middle = left + ((right - left) >> 1);
// 若当前middle所指为target将左边界设为middle并向左继续寻找左边界
if (nums[middle] == target) {
leftBorder = middle;
right = middle - 1;
} else if (nums[middle] > target) {
right = middle - 1;
} else {
left = middle + 1;
}
}
return leftBorder;
}
int searchRightBorder(int *nums, int numsSize, int target) {
int left = 0, right = numsSize - 1;
// 记录rightBorder没有被赋值的情况
int rightBorder = -1;
while (left <= right) {
int middle = left + ((right - left) >> 1);
// 若当前middle所指为target将右边界设为middle并向右继续寻找右边界
if (nums[middle] == target) {
rightBorder = middle;
left = middle + 1;
} else if (nums[middle] > target) {
right = middle - 1;
} else {
left = middle + 1;
}
}
return rightBorder;
}
int* searchRange(int* nums, int numsSize, int target, int* returnSize){
int leftBorder = searchLeftBorder(nums, numsSize, target);
int rightBorder = searchRightBorder(nums, numsSize, target);
// 定义返回数组及数组大小
*returnSize = 2;
int *resNums = (int*)malloc(sizeof(int) * 2);
resNums[0] = leftBorder;
resNums[1] = rightBorder;
return resNums;
}
```
<p align="center"> <p align="center">
<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"/>

View File

@ -151,6 +151,24 @@ function moveZeroes(nums: number[]): void {
}; };
``` ```
### C
```c
void moveZeroes(int* nums, int numsSize){
int fastIndex = 0, slowIndex = 0;
for (; fastIndex < numsSize; fastIndex++) {
if (nums[fastIndex] != 0) {
nums[slowIndex++] = nums[fastIndex];
}
}
// 将slowIndex之后的元素变为0
for (; slowIndex < numsSize; slowIndex++) {
nums[slowIndex] = 0;
}
}
```

View File

@ -241,6 +241,16 @@ impl<T> ListNode<T> {
``` ```
### C:
```c
typedef struct ListNodeT {
int val;
struct ListNodeT next;
} ListNode;
```
<p align="center"> <p align="center">
<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"/>