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 a rotated sorted array

二分查找是必须要掌握的技能。二分查找适用于有序的、顺序的存储结构。 1、可以用它来查找某一个数 2、可以用于查找某一个范围。如《二分查找有序数组中某个数的所在范围 Search for a Rang...

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

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

Search in Rotated Sorted Array--LeetCode

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6...

#62 Search in Rotated Sorted Array

题目描述: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 ...

LeetCode Algorithms 33. Search in Rotated Sorted Array

题目难度: Medium 原题描述: Suppose an array sorted in ascending order is rotated at some pivot unk...

[LeetCode] Search in Rotated Sorted Array [35]

旋转数组中的查找。[1, 2, 3, 4, 5, 6]的一个旋转数组为[4, 5, 6, 1, 2, 3]。在旋转数组中寻找一个数。 最直接的方法,一次遍历,时间复杂度O(n)。但是既然是一个部分有序...
  • swagle
  • swagle
  • 2014年06月13日 11:57
  • 919

leetcode_033 Search in Rotated Sorted Array

题目分析: 在旋转后的有序数组中查找某个元素。 解题思路: 两种方法实现方法1:遍历实现1)先判断要查找的数与数组头元素和数组尾元素大小,确定遍历方向;2)以一定的方向遍历查找,判断是否存在次元素。注...

LeetCode-Search in Rotated Sorted Array

class Solution { public: int search(int A[], int n, int target) { // Start typing you...

76-Search in Rotated Sorted Array

Search in Rotated Sorted Array My Submissions QuestionEditorial Solution Total Accepted: 102489 Tot...

leetcode-33-Search in Rotated Sorted Array

#include #include using namespace std; class Solution { public: /* 旋转数组只能有三种情况: 1. 左...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Search in Rotated Sorted Array
举报原因:
原因补充:

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