Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
int search(int* nums, int numsSize, int target) {
int i;
for(i=0;i<numsSize;i++)
{
if(nums[i]==target) return i;
}
return -1;
}
Search in Rotated Sorted Array II
Follow up for “Search in Rotated Sorted Array”:
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
bool search(int* nums, int numsSize, int target) {
int i;
for(i=0;i<numsSize;i++)
{
if(nums[i]==target) return true;
}
return false;
}
这两道题 !!虽然AC,但是是胡闹,需要重新解!!
本文深入解析了搜索旋转排序数组的问题,并针对允许重复元素的情况进行了拓展讨论,提供了相应的解决策略。
227

被折叠的 条评论
为什么被折叠?



