#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static bool search(int* nums, int numsSize, int target)
{
int lo = 0;
int hi = numsSize - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (nums[mid] == target) {
return true;
}
if (nums[lo] == nums[mid] && nums[mid] == nums[hi]) {
/* Not sure which side contains the peak value, reduce search range */
lo++;
hi--;
} else if (nums[lo] <= nums[mid]) { /* lo might be equal to mid */
if (nums[lo] <= target && target < nums[mid]) {
hi = mid - 1;
} else {
lo = mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[hi]) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
}
return false;
}
int main(int argc, char **argv)
{
int i;
int target = atoi(argv[1]);
int size = argc - 2;
int *nums = malloc(size * sizeof(int));
for (i = 0; i < argc - 2; i++) {
nums[i] = atoi(argv[i + 2]);
}
printf("%d\n", search(nums, size, target));
return 0;
}
C语言基础-leetcode编程题解之第81题搜索旋转排序数组II.zip
需积分: 1 14 浏览量
2024-07-02
05:00:15
上传
评论
收藏 2KB ZIP 举报
![avatar](https://profile-avatar.csdnimg.cn/3c54a849f48044a884c4cf76b8fda72a_weixin_66442839.jpg!1)
__AtYou__
- 粉丝: 2098
- 资源: 710
最新资源
- Screenshot_20240703_230017_com.tencent.mm.jpg
- com.shouzhi.pndr2-1_sign.apk
- python-leetcode python题解之第654题最大二叉树
- WebBrowser控件的常用方法、属性和事件
- python-leetcode python题解之第628题三个数的最大乘积
- python-leetcode python题解之第617题合并二叉树
- python-leetcode python题解之第605题种花问题
- python-leetcode python题解之第581题最短无序连续子数组
- python-leetcode python题解之第572题另一棵树的子树
- python-leetcode python题解之第560题和为K的子数组
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)