[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., [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旋转过有序数组里找目标值的更多相关文章
- LeetCode 33 Search in Rotated Sorted Array(循环有序数组中进行查找操作)
题目链接 :https://leetcode.com/problems/search-in-rotated-sorted-array/?tab=Description Problem :当前的数组 ...
- [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. 思路 ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- [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. ...
- 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. ...
- 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 ...
- 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 ...
- 33. Search in Rotated Sorted Array旋转数组二分法查询
一句话思路:反正只是寻找一个最小区间,断开也能二分.根据m第一次的落点,来分情况讨论. 一刷报错: 结构上有根本性错误:应该是while里面包括if,不然会把代码重复写两遍,不好. //situati ...
随机推荐
- java_oop_关键字
空白符注释 标识符分隔符操作符字面量关键字 instanceof java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出, ...
- 第一章 HTML+CSS(中)
4.域元素(form表单.textarea文本域.fieldset域集合.input使用) 案例 表单 用户名: 密码: 昵称: 你喜欢的水果有? 苹果 黄瓜 香蕉 请选择性别 男 女 请选择你要的网 ...
- redis 的备份策略,最好使用:RDB-AOF 混合持久化
相关资料: Redis 4.0 新功能简介:RDB-AOF 混合持久化:http://blog.huangz.me/2017/redis-rdb-aof-mixed-persistence.html ...
- 计算:表中varchar类型的字段能容纳的最大字符数?
建表语句: CREATE TABLE `test2` ( `id` int unsigned NOT NULL AUTO_INCREMENT, `content` varchar(21842) NOT ...
- centos-rpm安装的mariadb,php52源码编译安装时注意点
1.不要静态指定with-mysql 以扩展的mysql.so的形式安装 2.找不到header file之类的 要yum install mysql-devel find / -name mysql ...
- VDMA时序分析
VDMA时序分析
- springboot对oracle的配置
spring.jpa.database=oracle spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver sprin ...
- selenium +chromdriver模块
1 selenium 模拟浏览器行为 2 chromdriver 对应的chrome浏览器驱动 下载地址 注意:chrome与chromdriver存在对应关系 以下错误就可能是版本不对应 ...
- [UE4]RichTextBlock
RichTextBlock:富文本 一.新建一个名为“TestRichTextBlock_0”的UserWidget,并添加名为RichTextBlock_0的RichTextBlock控件,并设置T ...
- 02-Introspector内省设置单个属性
package com.oa.test; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; ...