Search in Rotated Sorted Array

原创 2016年08月30日 14:44:47

**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.

Subscribe to see which companies asked this question
**


C++

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int first = 0;
        int last = nums.size();
        while(first != last)
        {
            const int mid = first + (last - first) / 2;
            if(nums[mid] == target)
                return mid;
            if(nums[first] <= nums[mid])
            {
                if(nums[first] <= target && target < nums[mid])
                    last = mid;
                else
                    first = mid + 1;
            }
            else
            {
                if(nums[last-1] >= target && target > nums[mid])
                    first = mid + 1;
                else
                    last = mid;
            }
        }
        return -1;
    }
};

Java

public class Solution {
    public int search(int[] nums, int target) {
        int first = 0;
        int last = nums.length;

        while(first != last)
        {
            int mid = first + (last - first) / 2;
            if(nums[mid] == target)
                return mid;
            if(nums[first] <= nums[mid])
            {
                if(nums[first] <= target && target < nums[mid])
                    last = mid;
                else
                    first = mid + 1;
            }
            else
            {
                if(nums[last-1] >= target && target > nums[mid])
                    first = mid  + 1;
                else
                    last = mid;
            }
        }
        return -1;
    }
}

Python

class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        first = 0
        last = len(nums)
        while(first != last):
            mid = first + (last - first) / 2;
            if(nums[mid] == target):
                return mid
            if(nums[mid] >= nums[first]):
                if(nums[first] <= target and target < nums[mid]):
                    last = mid
                else:
                    first = mid + 1
            else:
                if(nums[mid] < target and target <= nums[last-1]):
                    first = mid + 1
                else:
                    last = mid
        return -1
版权声明:本文为博主原创文章,未经博主允许不得转载。

Search in Rotated Sorted Array -- LeetCode

原题链接: http://oj.leetcode.com/problems/search-in-rotated-sorted-array/  这道题是二分查找Search Insert Positi...
  • linhuanmars
  • linhuanmars
  • 2014年03月05日 09:15
  • 19296

Search in Rotated Sorted Array II -- LeetCode

原题链接: http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/  这道题是二分查找Search Insert Pos...
  • linhuanmars
  • linhuanmars
  • 2014年03月06日 02:04
  • 17855

【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 ...
  • ljiabin
  • ljiabin
  • 2014年10月25日 17:00
  • 9803

[C++]LeetCode: 43 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 ...
  • cinderella_niu
  • cinderella_niu
  • 2014年12月11日 21:54
  • 595

【LEETCODE】33-Search in Rotated Sorted Array [Python]

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 mig...
  • aliceyangxi1987
  • aliceyangxi1987
  • 2016年01月22日 11:26
  • 1005

[LeetCode]81.Search in Rotated Sorted Array II

【题目】 Search in Rotated Sorted Array II  Total Accepted: 3749 Total Submissions: 12937My Submissi...
  • SunnyYoona
  • SunnyYoona
  • 2014年01月15日 22:29
  • 2505

leetcode 81. Search in Rotated Sorted Array II-旋转数组|二分查找

原题链接:81. Search in Rotated Sorted Array II 【思路-Java、Python】 二分查找实现 本题采用二分法实现,但是比较挠头的是边界问题,而且元素有重复,...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016年06月07日 11:34
  • 1438

[LeetCode]33.Search in Rotated Sorted Array

【题目】 Search in Rotated Sorted Array  Total Accepted: 5827 Total Submissions: 20925My Submissions...
  • SunnyYoona
  • SunnyYoona
  • 2014年01月15日 17:18
  • 2559

LeetCode 33 — Search in Rotated Sorted Array(C++ Java Python)

题目:http://oj.leetcode.com/problems/search-in-rotated-sorted-array/ Suppose a sorted array is rotate...
  • dragon_dream
  • dragon_dream
  • 2014年04月03日 14:20
  • 1526

[leetcode] 33. Search in Rotated Sorted Array 解题报告

题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array/ Suppose a sorted array is rotat...
  • qq508618087
  • qq508618087
  • 2016年01月03日 14:14
  • 435
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Search in Rotated Sorted Array
举报原因:
原因补充:

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