Skip to content

Commit daa159e

Browse files
committed
81. Search in Rotated Sorted Array II
1 parent a18f3df commit daa159e

File tree

4 files changed

+191
-0
lines changed

4 files changed

+191
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
### [81\. Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)
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,0,1,2,2,5,6]` might become `[2,5,6,0,0,1,2]`).
9+
10+
You are given a target value to search. If found in the array return `true`, otherwise return `false`.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: nums = [2,5,6,0,0,1,2], target = 0
16+
Output: true
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: nums = [2,5,6,0,0,1,2], target = 3
23+
Output: false```
24+
25+
**Follow up:**
26+
27+
* This is a follow up problem to , where `nums` may contain duplicates.
28+
* Would this affect the run-time complexity? How and why?
29+
30+
31+
#### Solution
32+
33+
Language: **Java**
34+
35+
```java
36+
class Solution {
37+
   public boolean search(int[] nums, int target) {
38+
       int low = 0;
39+
       int high = nums.length - 1;
40+
       int middle;
41+
       while (low <= high) {
42+
           middle = (high + low) / 2;
43+
           if (nums[middle] == target) {
44+
               return true;
45+
          }
46+
           if (nums[low] < nums[middle] || nums[high] < nums[middle]) { // [low,middle]是升序 或者是[middle,high]
47+
               if (target >= nums[low] && nums[middle] >= target) { // 普通二分查找就能得到结果
48+
                   high = middle - 1;
49+
              } else {
50+
                   low = middle + 1;
51+
              }
52+
53+
          } else if (nums[high] > nums[middle] || nums[low] > nums[middle]) { // [low,middle] 必然有对称点
54+
               if (target <= nums[high] && nums[middle] <= target) { // 普通二分查找就能得到结果
55+
                   low = middle + 1;
56+
              } else {
57+
                   high = middle - 1;
58+
              }
59+
60+
          } else { // nums[low] == nums[middle] 无法判断是升序还是有对称点
61+
               high--;
62+
          }
63+
      }
64+
       return false;
65+
  }
66+
}
67+
```
68+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-07-29-fwUbnO.jpg)

src/main/java/leetcode/_81_/Main.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leetcode._81_;
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+
int[] nums = {2, 5, 6, 0, 0, 1, 2};
10+
System.out.println(solution.search(nums, 2));
11+
System.out.println(solution.search(nums, 5));
12+
System.out.println(solution.search(nums, 6));
13+
System.out.println(solution.search(nums, 0));
14+
System.out.println(solution.search(nums, 1));
15+
System.out.println(solution.search(nums, 2));
16+
int[] nums2 = {2,5,6,0,0,1,2};
17+
System.out.println(solution.search(nums2, 3));
18+
}
19+
}
20+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package leetcode._81_;
2+
3+
import java.util.Arrays;
4+
5+
class Solution {
6+
public boolean search(int[] nums, int target) {
7+
int low = 0;
8+
int high = nums.length - 1;
9+
int middle;
10+
while (low <= high) {
11+
middle = (high + low) / 2;
12+
if (nums[middle] == target) {
13+
return true;
14+
}
15+
if (nums[low] < nums[middle] || nums[high] < nums[middle]) { // [low,middle]是升序 或者是[middle,high]
16+
if (target >= nums[low] && nums[middle] >= target) { // 普通二分查找就能得到结果
17+
high = middle - 1;
18+
} else {
19+
low = middle + 1;
20+
}
21+
22+
} else if (nums[high] > nums[middle] || nums[low] > nums[middle]) { // [low,middle] 必然有对称点
23+
if (target <= nums[high] && nums[middle] <= target) { // 普通二分查找就能得到结果
24+
low = middle + 1;
25+
} else {
26+
high = middle - 1;
27+
}
28+
29+
} else { // nums[low] == nums[middle] 无法判断是升序还是有对称点
30+
high--;
31+
}
32+
}
33+
return false;
34+
}
35+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
### [81\. Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)
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,0,1,2,2,5,6]` might become `[2,5,6,0,0,1,2]`).
9+
10+
You are given a target value to search. If found in the array return `true`, otherwise return `false`.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: nums = [2,5,6,0,0,1,2], target = 0
16+
Output: true
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: nums = [2,5,6,0,0,1,2], target = 3
23+
Output: false```
24+
25+
**Follow up:**
26+
27+
* This is a follow up problem to , where `nums` may contain duplicates.
28+
* Would this affect the run-time complexity? How and why?
29+
30+
31+
#### Solution
32+
33+
Language: **Java**
34+
35+
```java
36+
class Solution {
37+
   public boolean search(int[] nums, int target) {
38+
       int low = 0;
39+
       int high = nums.length - 1;
40+
       int middle;
41+
       while (low <= high) {
42+
           middle = (high + low) / 2;
43+
           if (nums[middle] == target) {
44+
               return true;
45+
          }
46+
           if (nums[low] < nums[middle] || nums[high] < nums[middle]) { // [low,middle]是升序 或者是[middle,high]
47+
               if (target >= nums[low] && nums[middle] >= target) { // 普通二分查找就能得到结果
48+
                   high = middle - 1;
49+
              } else {
50+
                   low = middle + 1;
51+
              }
52+
53+
          } else if (nums[high] > nums[middle] || nums[low] > nums[middle]) { // [low,middle] 必然有对称点
54+
               if (target <= nums[high] && nums[middle] <= target) { // 普通二分查找就能得到结果
55+
                   low = middle + 1;
56+
              } else {
57+
                   high = middle - 1;
58+
              }
59+
60+
          } else { // nums[low] == nums[middle] 无法判断是升序还是有对称点
61+
               high--;
62+
          }
63+
      }
64+
       return false;
65+
  }
66+
}
67+
```
68+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-07-29-fwUbnO.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