-
Notifications
You must be signed in to change notification settings - Fork 30
/
SearchinRotatedSortedArray.cs
executable file
·53 lines (50 loc) · 3.07 KB
/
SearchinRotatedSortedArray.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Source : https://leetcode.com/problems/search-in-rotated-sorted-array/
// Author : codeyu
// Date : Monday, October 17, 2016 7:36:27 PM
/**********************************************************************************
*
* 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.
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
namespace Algorithms
{
public class Solution033
{
public static int Search(int[] nums, int target)
{
if(nums==null || nums.Length==0) return -1;
int l = 0;
int r = nums.Length-1;
while(l<=r)
{
int m = l + (r-l) / 2;
if(target == nums[m])
return m;
if(nums[m]<nums[r])
{
if(target>nums[m] && target<=nums[r])
l = m+1;
else
r = m-1;
}
else
{
if(target>=nums[l] && target<nums[m])
r = m-1;
else
l = m+1;
}
}
return -1;
}
}
}