Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 8 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Sep 10, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
添加 0034.在排序数组中查找元素的第一个和最后一个位置.md C语言解法
  • Loading branch information
KingArthur0205 committed Aug 22, 2023
commit 88ca946a7dd25e78eb50730a1285476691152337
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down

AltStyle によって変換されたページ (->オリジナル) /