forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
luzhipeng
committed
Jun 25, 2019
1 parent
5eb2608
commit 89695f7
Showing
6 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<mxfile modified="2019-06-25T08:29:45.719Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" etag="mL01lY1iAOqhLc-0r-Ae" version="10.8.0" type="device"><diagram id="957mmXiTEQiwaOJpB0L8" name="第 1 页">7Vtdj6M2FP01fkwENubjETKw1WorrTSqOm8VA06ChuAUPJukv762YxLAzO5MmwSPlDwkcLGNOef4+vo6ALTY7L/U6Xb9O81JCaCV7wF6ABB62OHfwnA4GvwAHQ2rusiPJvtseCz+IcpoKetrkZOmV5BRWrJi2zdmtKpIxnq2tK7prl9sScv+XbfpimiGxywtdeufRc7W6imgd7b/RorVur2z7QbHK5u0LayepFmnOd11TCgGaFFTyo5Hm/2ClAK7FpdjveSNq6eO1aRi76nwPfn68uQ/fcu+RtV65y6f/vjy90y18iMtX9UDu6q37NBCUNPXKieiFQugaLcuGHncppm4uuOcc9uabUp+ZvND1R6pGdm/2VH79PhcNoRuCKsPvEhbwVWIHVpM1fnuTABWpnUH+9aWKspXp5bPqPADBcwHQIIaSN7kIJ0GiCkgIQ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/rmwj2t+nHHqkUZHrQnT6s6I9shQ97aPGWxkW8HdAkcz3T+MkEqU6tRmr2sJDfdfyDJDy8ibxY22+NbDIKDtD1ZFntBYKT687BmTLz+EAokYJLlFZoXGa2WBWe9nmf8jjDJU5byH2Fv+O+yJmSG8tmqpE1zmMm08ZKLZiYKzBoRYyUul33yQErCyHxbrYbS0f9ydkMxofZPOK2URrNenqMrqbV9QEn89PxehrzWebkFxf8C</diagram></mxfile> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
## 题目地址 | ||
https://leetcode.com/problems/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 | ||
Example 2: | ||
Input: nums = [4,5,6,7,0,1,2], target = 3 | ||
Output: -1 | ||
``` | ||
|
||
## 思路 | ||
|
||
这是一个我在网上看到的前端头条技术终面的一个算法题。 | ||
|
||
题目要求时间复杂度为logn,因此基本就是二分法了。 这道题目不是直接的有序数组,不然就是easy了。 | ||
|
||
首先要知道,我们随便选择一个点,将数组分为前后两部分,其中一部分一定是有序的。 | ||
|
||
具体步骤: | ||
|
||
- 我们可以先找出mid,然后根据mid来判断,mid是在有序的部分还是无序的部分 | ||
|
||
假如mid小于start,则mid一定在右边有序部分。 | ||
假如mid大于等于start, 则mid一定在左边有序部分。 | ||
|
||
> 注意等号的考虑 | ||
- 然后我们继续判断target在哪一部分, 我们就可以舍弃另一部分了 | ||
|
||
我们只需要比较target和有序部分的边界关系就行了。 比如mid在右侧有序部分,即[mid, end] | ||
那么我们只需要判断 target >= mid && target <= end 就能知道target在右侧有序部分,我们就 | ||
可以舍弃左边部分了(start = mid + 1), 反之亦然。 | ||
|
||
我们以([6,7,8,1,2,3,4,5], 4)为例讲解一下: | ||
|
||
![search-in-rotated-sorted-array-1](../assets/problems/search-in-rotated-sorted-array-1.jpg) | ||
|
||
|
||
![search-in-rotated-sorted-array-1](../assets/problems/search-in-rotated-sorted-array-2.jpg) | ||
|
||
|
||
## 关键点解析 | ||
|
||
- 二分法 | ||
- 找出有序区间,然后根据target是否在有序区间舍弃一半元素 | ||
## 代码 | ||
|
||
* 语言支持: Javascript | ||
|
||
```js | ||
/* | ||
* @lc app=leetcode id=33 lang=javascript | ||
* | ||
* [33] Search in Rotated Sorted Array | ||
*/ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number} | ||
*/ | ||
var search = function(nums, target) { | ||
// 时间复杂度:O(logn) | ||
// 空间复杂度:O(1) | ||
// [6,7,8,1,2,3,4,5] | ||
let start = 0; | ||
let end = nums.length - 1; | ||
|
||
while (start <= end) { | ||
const mid = start + ((end - start) >> 1); | ||
if (nums[mid] === target) return mid; | ||
|
||
// [start, mid]有序 | ||
|
||
// ️⚠️注意这里的等号 | ||
if (nums[mid] >= nums[start]) { | ||
//target 在 [start, mid] 之间 | ||
|
||
// 其实target不可能等于nums[mid], 但是为了对称,我还是加上了等号 | ||
if (target >= nums[start] && target <= nums[mid]) { | ||
end = mid - 1; | ||
} else { | ||
//target 不在 [start, mid] 之间 | ||
start = mid + 1; | ||
} | ||
} else { | ||
// [mid, end]有序 | ||
|
||
// target 在 [mid, end] 之间 | ||
if (target >= nums[mid] && target <= nums[end]) { | ||
start = mid + 1; | ||
} else { | ||
// target 不在 [mid, end] 之间 | ||
end = mid - 1; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
}; | ||
``` | ||
|
||
## 扩展 | ||
|
||
|
||
|