您的位置:首页 > 其它

!leetcode[33&81]:Search in Rotated Sorted Array[I & II]

2015-06-26 17:04 681 查看
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,但是是胡闹,需要重新解!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  array