File tree 3 files changed +92
-0
lines changed
src/com/blankj/medium/_033
3 files changed +92
-0
lines changed Original file line number Diff line number Diff line change 63
63
| 15| [ 3Sum] [ 015 ] | Array, Two Pointers|
64
64
| 17| [ Letter Combinations of a Phone Number] [ 017 ] | String, Backtracking|
65
65
| 19| [ Remove Nth Node From End of List] [ 019 ] | Linked List, Two Pointers|
66
+ | 33| [ Search in Rotated Sorted Array] [ 033 ] | Arrays, Binary Search|
66
67
| 554| [ Brick Wall] [ 554 ] | Hash Table|
67
68
68
69
122
123
[ 015 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/015/README.md
123
124
[ 017 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/017/README.md
124
125
[ 019 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md
126
+ [ 033 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/033/README.md
125
127
[ 554 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/554/README.md
126
128
127
129
[ 004 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/004/README.md
Original file line number Diff line number Diff line change
1
+ # [ Search in Rotated Sorted Array] [ title ]
2
+
3
+ ## Description
4
+
5
+ Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
6
+
7
+ (i.e., ` 0 1 2 4 5 6 7 ` might become ` 4 5 6 7 0 1 2 ` ).
8
+
9
+ You are given a target value to search. If found in the array return its index, otherwise return -1.
10
+
11
+ You may assume no duplicate exists in the array.
12
+
13
+ ** Tags:** Arrays, Binary Search
14
+
15
+
16
+ ## 思路
17
+
18
+ 题意是让你从一个旋转过后的递增序列中寻找给定值,找到返回索引,找不到返回-1,我们在下面这组数据中寻找规律。
19
+ ```
20
+ 1 2 4 5 6 7 0
21
+ 2 4 5 6 7 0 1
22
+ 4 5 6 7 0 1 2
23
+ 5 6 7 0 1 2 4
24
+ 6 7 0 1 2 4 5
25
+ 7 0 1 2 4 5 6
26
+ ```
27
+ 由于是旋转一次,所以肯定有一半及以上的序列仍然是具有递增性质的,我们观察到如果中间的数比左面的数大的话,那么左半部分序列是递增的,否则右半部分就是递增的,那么我们就可以确定给定值是否在递增序列中,从而决定取舍哪半边。
28
+
29
+
30
+ ``` java
31
+ class Solution {
32
+ public int search (int [] nums , int target ) {
33
+ int l = 0 , r = nums. length - 1 , mid;
34
+ while (l <= r) {
35
+ mid = l + r >>> 1 ;
36
+ if (nums[mid] == target) return mid;
37
+ else if (nums[mid] >= nums[l]) {
38
+ if (nums[l] <= target && target < nums[mid]) r = mid - 1 ;
39
+ else l = mid + 1 ;
40
+ } else {
41
+ if (nums[mid] < target && target <= nums[r]) l = mid + 1 ;
42
+ else r = mid - 1 ;
43
+ }
44
+ }
45
+ return - 1 ;
46
+ }
47
+ }
48
+ ```
49
+
50
+
51
+ ## 结语
52
+
53
+ 如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[ awesome-java-leetcode] [ ajl ]
54
+
55
+
56
+
57
+ [ title ] : https://leetcode.com/problems/search-in-rotated-sorted-array
58
+ [ ajl ] : https://github.com/Blankj/awesome-java-leetcode
Original file line number Diff line number Diff line change
1
+ package com .blankj .medium ._033 ;
2
+
3
+ /**
4
+ * <pre>
5
+ * author: Blankj
6
+ * blog : http://blankj.com
7
+ * time : 2017/10/16
8
+ * desc :
9
+ * </pre>
10
+ */
11
+ public class Solution {
12
+ public int search (int [] nums , int target ) {
13
+ int l = 0 , r = nums .length - 1 , mid ;
14
+ while (l <= r ) {
15
+ mid = l + r >>> 1 ;
16
+ if (nums [mid ] == target ) return mid ;
17
+ else if (nums [mid ] >= nums [l ]) {
18
+ if (nums [l ] <= target && target < nums [mid ]) r = mid - 1 ;
19
+ else l = mid + 1 ;
20
+ } else {
21
+ if (nums [mid ] < target && target <= nums [r ]) l = mid + 1 ;
22
+ else r = mid - 1 ;
23
+ }
24
+ }
25
+ return -1 ;
26
+ }
27
+
28
+ public static void main (String [] args ) {
29
+ Solution solution = new Solution ();
30
+ System .out .println (solution .search (new int []{2 , 1 }, 1 ));
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments