Skip to content

Commit 913a753

Browse files
committed
33. Search in Rotated Sorted Array
1 parent 402d290 commit 913a753

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

src/leetcode/_33_/Main.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode._33_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println(solution.search(new int[]{5, 1, 3}, 3));
10+
}
11+
}
12+

src/leetcode/_33_/Solution.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package leetcode._33_;
2+
3+
import java.util.Arrays;
4+
5+
class Solution {
6+
public int search(int[] nums, int target) {
7+
int low = 0;
8+
int high = nums.length - 1;
9+
while (low <= high) {
10+
int mid = (low + high) / 2;
11+
if (nums[mid] == target) {
12+
return mid;
13+
}
14+
if (nums[mid] >= nums[low]) { //说明low到mid这一段是递增的,不会经过对称线
15+
if (target >= nums[low] && target <= nums[mid]) { // 如果 target 在 这之间,继续二分即可
16+
int result = Arrays.binarySearch(nums, low, mid, target);
17+
return result >= 0 ? result : -1;
18+
} else {
19+
low = mid + 1;
20+
}
21+
} else {
22+
if (target >= nums[mid] && target <= nums[high]) { // 如果 target 在 这之间,继续二分即可
23+
int result = Arrays.binarySearch(nums, mid + 1, high + 1, target);
24+
return result >= 0 ? result : -1;
25+
} else {
26+
high = mid - 1;
27+
}
28+
}
29+
}
30+
return -1;
31+
}
32+
}

src/leetcode/_33_/solution.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
### [33\. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
7+
8+
(i.e., `[0,1,2,4,5,6,7]` might become `[4,5,6,7,0,1,2]`).
9+
10+
You are given a target value to search. If found in the array return its index, otherwise return `-1`.
11+
12+
You may assume no duplicate exists in the array.
13+
14+
Your algorithm's runtime complexity must be in the order of _O_(log _n_).
15+
16+
**Example 1:**
17+
18+
```
19+
Input: nums = [4,5,6,7,0,1,2], target = 0
20+
Output: 4
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: nums = [4,5,6,7,0,1,2], target = 3
27+
Output: -1```
28+
29+
30+
#### Solution
31+
32+
Language: **Java**
33+
34+
```java
35+
class Solution {
36+
   public int search(int[] nums, int target) {
37+
       int low = 0;
38+
       int high = nums.length - 1;
39+
       while (low <= high) {
40+
           int mid = (low + high) / 2;
41+
           if (nums[mid] == target) {
42+
               return mid;
43+
          }
44+
           if (nums[mid] >= nums[low]) { //说明low到mid这一段是递增的,不会经过对称线
45+
               if (target >= nums[low] && target <= nums[mid]) { // 如果 target 在 这之间,继续二分即可
46+
                   int result = Arrays.binarySearch(nums, low, mid, target);
47+
                   return result >= 0 ? result : -1;
48+
              } else {
49+
                   low = mid + 1;
50+
              }
51+
          } else {
52+
               if (target >= nums[mid] && target <= nums[high]) { // 如果 target 在 这之间,继续二分即可
53+
                   int result = Arrays.binarySearch(nums, mid + 1, high + 1, target);
54+
                   return result >= 0 ? result : -1;
55+
              } else {
56+
                   high = mid - 1;
57+
              }
58+
          }
59+
      }
60+
       return -1;
61+
  }
62+
}
63+
```
64+
![](https://ws1.sinaimg.cn/large/006tKfTcgy1g12lb7o7nfj30u00v5wk7.jpg)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy