Suppose an array sorted in ascending order 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.

Your algorithm's runtime complexity must be in the order of O(log n).

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

题目

一个有序数组,可能进行了循环移位,在里面进行查找。

Solution1: Two Pointers(left&right)

1. We can observe even rotating the array,  at least one part of such array is sorted.

2. Find such sorted array part and do binary search

3. how to find such sorted array?

(1) use two pointers: left, right

(2) if nums[left] < nums[mid],  we can say from left to mid, it is in ascending order, then left part is sorted

(3) if nums[left] > nums[mid], we can say from left to mid, it is NOT in ascending order, then left part is NOT sorted

code

 /*
Time: O(logn). We use binary search
Space:O(1) . We only used constant extra space.
*/ class Solution {
public int search(int[] nums, int target) {
// corner case
if (nums.length == 0) return -1;
// initialize
int left = 0;
int right = nums.length - 1; while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) return mid;
// left part is not in ascending order
else if (nums[mid] < nums[left]) {
if (target > nums[mid] && target <= nums[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
// nums[mid] >= nums[left] left side is in ascending order
else {
if (target < nums[mid] && target >= nums[left]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
}
return -1;
}
}

[leetcode]33. Search in Rotated Sorted Array旋转过有序数组里找目标值的更多相关文章

  1. LeetCode 33 Search in Rotated Sorted Array(循环有序数组中进行查找操作)

    题目链接 :https://leetcode.com/problems/search-in-rotated-sorted-array/?tab=Description   Problem :当前的数组 ...

  2. [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)

    This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...

  3. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  4. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

  5. [LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  6. LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  7. leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法

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

  8. leetcode 33. 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 migh ...

  9. 33. Search in Rotated Sorted Array旋转数组二分法查询

    一句话思路:反正只是寻找一个最小区间,断开也能二分.根据m第一次的落点,来分情况讨论. 一刷报错: 结构上有根本性错误:应该是while里面包括if,不然会把代码重复写两遍,不好. //situati ...

随机推荐

  1. C++对象的构造、析构与拷贝构造

    今天下午在研究虚函数的时候遇到了一个问题,觉得很有意思,记录一下. 先看代码: class Base { public: Base(int value) { m_nValue = value; cou ...

  2. Go语言开发Windows应用

    Go语言开发Windows应用 当第一次看到Go程序在windows平台生成可执行的exe文件,就宣告了windows应用也一定是Go语言的战场.Go不是脚本语言,但却有着脚本语言的轻便简单的特性.相 ...

  3. Vue-项目之免费课和购物车实现

    调整首页细节 固定头部 App.vue中代码 <style> body{ padding: 0; margin:0; margin-top: 80px; } </style> ...

  4. 3.3-1933 problem A

    #include <stdio.h> int main(void){ int h; while(scanf("%d", &h) != EOF){ * (h-); ...

  5. Fabric的@runs_once的理解

    1:runs_once的用法,一直没理解,我看网上都是说:“函数修饰符,标识的函数只会执行一次,不受多台主机影响”     实在没理解,然后看了一下官方文档,这样解释     举个例子: #!/usr ...

  6. Spring Cloud(Dalston.SR5)--Config 集群配置中心

    Spring Cloud Config 是一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他分为服务端和客户端两个部分.服务端也称为分布式配置中心,是一个独立的微服务 ...

  7. GradleUserGuide中文版 19)Plugins 20)插件规范 21)Java插件

    https://blog.csdn.net/roymuste/article/details/51321881

  8. 使用lite-server

    进入项目根目录,执行下列步骤 安装lite-server npm install lite-server 新建配置文件bs-config.json bs-config.json中可以: 指定监听的端口 ...

  9. VS2012 安装 NPOI (管理NuGet程序包)

    问题背景 选择项目后右键==>管理NuGet程序包,搜索NPOI,返回服务器无法找到...404 解决方法: 第一步: 访问:https://www.nuget.org/api/v2/      ...

  10. reids高可用(灾难备份-持久化)

    java缓存存放到内存之中,当服务器重启以后,内存的数据将丢失,而reids作为缓存,重启reids以后 数据是不是也会丢失,redis服务器重启以后数据也不会丢失,这个是redis提供了持久化的功能 ...