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. PythonStudy——三种字符串 Three strings

    # 普通字符串:u'以字符作为输出单位'print(u'abc') # 用于显示 # 二进制字符串:b'' 二进制字符串以字节作为输出单位print(b'abc') # 用于传输 # 原义字符串:r' ...

  2. edgedb 强大的对象关系数据库

    edgedb 是一个强大的对象关系数据库,构建在pg 之上. 包含的特性: 严格的强类型模式; 强大而富有表现力的查询语言; 丰富的标准库; 内置支持模式迁移; 本机GraphQL支持. 数据模型 从 ...

  3. [C#]typeof,Gettype()和is的区别

    typeof 参数是一个类型名称,比如你自己编写的一个类 GetType()是类的方法,继承自object,返回该实例的类型 is 用来检测实例的兼容性(是否可以相互转换) 例: class Anim ...

  4. 关于Xilinx AXI Lite 源代码分析---自建带AXI接口的IP

    关于Xilinx AXI Lite 源代码分析---自建带AXI接口的IP 首先需要注意此处寄存器数量的配置,它决定了slv_reg的个数. 读写数据,即是对寄存器slv_reg进行操作: 关于AXI ...

  5. maya中MFnMesh.h使用说明的翻译

    由于最近要修改一个maya中的deformer脚本,于是开始系统学习openMaya的一些知识,当然少不了得把一堆头文件说明看一遍.首先把MFnMesh.h这个文件翻译一下吧,不废话,上译文: 首先M ...

  6. 接口测试工具SoapUI Pro5.1.2基本使用20150920

    soapui是接口测试工具,最近因为要做接口测试,使用了下,现在和大家分享下: 工具安装很简单,就不说了,直接说使用,先什么都不说,照着操作一遍,我们拿天气预报的webserver来实战: 主要包括: ...

  7. SqlDbx 个人版本使用指定的instant client

    set Oracle_Home=D:\Toolkit\Oracle kits\instantclient_x86 set TNS_ADMIN=%Oracle_Home% set NLS_LANG=SI ...

  8. html5 + shiro

    偶然与巧合 舞动了蝶翼 谁的心头风起 前赴而后继 万千人追寻 荒漠唯一菩提 似擦肩相遇 或擦肩而去 命运犹如险棋 无数时间线 无数可能性 终于交织向你

  9. 第一课了解SQL

    1.1 数据库基础 其实一直在使用这数据库,当你在邮箱中查询名字时,当在网站上进行搜索,在自动取款机上使用ATM卡,这些操作都是在使用数据库 1.1.1 数据库   数据库:保存有组织的数据的容器(通 ...

  10. 502 Bad Gateway

    状态码解释: 502 Bad Gateway:作为网关或者代理工作的服务器尝试执行请求时,从上游服务器接收到无效的响应. 502 原因分析: 将请求提交给网关如php-fpm执行,但是由于某些原因没有 ...