Search in Rotated Sorted Array

原创 2016年08月30日 16:52:39
<pre name="code" class="cpp">class Solution{
	public:
	int search(vector<int>& nums,int target){
		int first=0,end=nums.size();
		int mid=0;
		while(first<end){
			mid=(first+end)/2;
			if(nums[mid]==target)
			return mid;
			if(nums[mid]>=nums[first]){
				if(target>=nums[first]&&target<nums[mid]){
					end=mid;
				}
				else{
					first=mid+1;
				}
				
			}
			else{
				if(target>nums[mid]&&target<=nums[end-1]){
					first=mid+1;
				}
				else{
					end=mid;
				}
			}
			
		}
		return -1;
	}
};



                    

相关文章推荐

leetcode解题之33&81. Search in Rotated Sorted Array java版(在旋转的数字中查找指定值)

33. Search in Rotated Sorted Array ,81. Search in Rotated Sorted Array II 。 leetcode解题之33&81. Search...

[LeetCode][Java] Search in Rotated Sorted Array II

题目: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t...

leetcode(33). Search in Rotated Sorted Array

problem Suppose an array sorted in ascending order is rotated at some pivot unknown to you befo...

LeetCode | 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 m...

LeetCode81——Search in Rotated Sorted Array II

LeetCode81——Search in Rotated Sorted Array II

LeetCode OJ 33 Search in Rotated Sorted Array

难度:hard 由此可见leetcode的hard难度比较于acm是偏简单一些的。 Suppose a sorted array is rotated at some pivot unkn...
  • LLL_mj
  • LLL_mj
  • 2015年07月06日 18:21
  • 295

[leetcode] 81.Search in Rotated Sorted Array II

题目: Follow up for “Search in Rotated Sorted Array”: What if duplicates are allowed?Would this affe...

35 leetcode - Search in Rotated Sorted Array

Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before...
  • lis_12
  • lis_12
  • 2016年12月10日 19:36
  • 101

LeetCode 之 Search in Rotated Sorted Array I II — C/C++ 实现

Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you befo...

※ Leetcode - Binary Search - 153. Find Minimum in Rotated Sorted Array(二分查找)

1. Problem Description Suppose a sorted array is rotated at some pivot unknown to you beforehand.   ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Search in Rotated Sorted Array
举报原因:
原因补充:

(最多只允许输入30个字)