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

来源:互联网 发布:json文件怎么添加注释 编辑:程序博客网 时间:2024/06/05 18:45

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,但是是胡闹,需要重新解!!

0 0
原创粉丝点击