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
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.
链接: http://leetcode.com/problems/search-in-rotated-sorted-array/
题解:Rotated array中查找值,使用Binary Search,要注意数组是向左shift或者是向右shift,保持在一个单调递增的部分进行查找。
简单的三种情况 - 1) 不平移 1234567
2) 向左3位 4567123
3) 向右3位 5671234
时间复杂度 O(logn), 空间复杂度 O(1)
public class Solution {
public int search(int[] nums, int target) { //binary search
if(nums == null || nums.length == 0)
return 0;
int lo = 0, hi = nums.length - 1; while(lo <= hi) {
int mid = lo + (hi - lo) / 2;
if(nums[mid] == target)
return mid;
else if(nums[mid] < nums[hi]) { // right half sorted
if(target > nums[mid] && target <= nums[hi]) //e.g. target = 5, 1-2-3-4-5-6-7, 6-7-1-2-3-4-5
lo = mid + 1;
else //e.g target = 5, 5-6-7-1-2-3-4
hi = mid - 1;
} else { // left half sorted
if(target < nums[mid] && target >= nums[lo]) //e.g. target = 3, 3-4-5-6-7-1-2
hi = mid - 1;
else //e.g. target = 3, 4-5-6-7-1-2-3
lo = mid + 1;
}
} return -1;
}
}
二刷:
Java:
反而写得比较复杂了。不过原理就是用nums[mid]和nums[right]进行比较,保持mid左边或者右边一段是有序的,再根据target与 nums[mid] 和nums[right]的关系来完成进一步的二分。 看到3天前StefanPochmann又发了一个帖子,可以加入Integer.MIN_VALUE或者Integer.MAX_VALUE来进行普通二分,非常巧妙,放在reference里以后看。
Time Complexity - O(logn), Space Complexity - O(1)
public class Solution {
public int search(int[] nums, int target) { // assume no duplicates
if (nums == null || nums.length == 0) {
return -1;
}
int lo = 0, hi = nums.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (target == nums[mid]) {
return mid;
} else if (target < nums[mid]) {
if (nums[mid] < nums[hi]) { // right part sorted
hi = mid - 1;
} else{
if (target <= nums[hi]) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
} else {
if (nums[mid] < nums[hi]) {
if (target > nums[hi]) {
hi = mid - 1;
} else {
lo = mid + 1;
}
} else {
lo = mid + 1;
}
}
}
return -1;
}
}
题外话:
今天被Two Sigma的Recruiter联系,聊了一下之后感觉对方十分专业。打算递交简历以后再拖延一段时间,来进行复习准备。更需要练习的是反应速度,脑力,分析能力,交流能力,专注程度,以及做题的速度和准确度。
三刷:
使用了一刷的逻辑。还是Binary Search,就是先判断是否nums[mid] == target,再判断这个sorted array是向左还是右平移,哪一部分仍然是有序的。接下来根据上面的信息来判断如何二分。
Java:
Time Complexity - O(logn), Space Complexity - O(1)。
public class Solution {
public int search(int[] nums, int target) {
if (nums == null || nums.length == 0) return -1;
int lo = 0, hi = nums.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (target == nums[mid]) {
return mid;
} else if (nums[mid] < nums[lo]) { // right side sorted
if (target > nums[mid] && target <= nums[hi]) lo = mid + 1;
else hi = mid - 1;
} else { // left side sorted
if (target >= nums[lo] && target < nums[mid]) hi = mid - 1;
else lo = mid + 1;
}
}
return -1;
}
}
4/3/2016: 今天去Edison取税,之后和地图君一块吃了一家H-Mart附近的东北菜馆。人很少,周日中午加上我们一共就三桌,不过味道挺不错。 吃了烤羊肉串,豆腐脑,煎饼果子,韭菜盒子,锅包肉,地三鲜,酸菜排骨等等,特别对我的胃口。咸豆腐脑可能还是来美帝这么久第一次吃到,好羡慕住附近的朋友们。地图君坚持付了帐,下次再这样我要生气了。
Reference:
https://leetcode.com/discuss/80659/clever-idea-making-it-simple
https://leetcode.com/discuss/41134/java-ac-solution-using-once-binary-search
33. Search in Rotated Sorted Array的更多相关文章
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...
- [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
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
- 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- 刷题33. Search in Rotated Sorted Array
一.题目说明 这个题目是33. Search in Rotated Sorted Array,说的是在一个"扭转"的有序列表中,查找一个元素,时间复杂度O(logn). 二.我的解 ...
- [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 ...
随机推荐
- python: 生成guid
其实经常需要生成一个guid,在各种场合使用...也简单写个小脚本实现吧. 实现下来发现速度比较慢... import uuid import sys def show_ver(): print 'g ...
- Oracle varchar2 4000
关于oracle varchar2 官方文档的描述 VARCHAR2 Data Type The VARCHAR2 data type specifies a variable-length char ...
- 1014. Waiting in Line (30)
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...
- MinGW-64 安装
一.在mingw-w64官网下载mingw-w64在线安装包 二.点击mingw-w64进行安装,选择: Version:选最新版本 我这个是4.9.2 Architecture:x86_64 (64 ...
- 转:一份基础的嵌入式Linux工程师笔试题
一. 填空题: 1. 一些Linux命令,显示文件,拷贝,删除 Ls cp rm 2. do……while和while……do有什么区别? 3. Linux系统下.ko文件是什么文件?.so文件是什么 ...
- 从InputStream到String_写成函数
String result = readFromInputStream(inputStream);//调用处 //将输入流InputStream变为String public String readF ...
- XML学习总结
什么是XML?XML指可扩展标记语言(EXtendsible Markup Language) XML的设计宗旨是传输数据,而不是显示数据. XML标签没有被预定义(html是预定义),XML里面您需 ...
- Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心
题目链接: 题目 E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes inputs ...
- 【tarjan】BZOJ 1051:受欢迎的牛
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3134 Solved: 1642[Submit][Sta ...
- hdu 1754 线段树入门
线段树点修改 区间最大值查询 #include <cstdio> #include <cstdlib> #include <cmath> #include < ...