|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
| 3 | +/** |
| 4 | + * 81. Search in Rotated Sorted Array II |
| 5 | + * <p> |
| 6 | + * There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values). |
| 7 | + * Before being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is |
| 8 | + * [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,4,4,5,6,6,7] might be rotated at pivot index 5 and become [4,5,6,6,7,0,1,2,4,4]. |
| 9 | + * Given the array nums after the rotation and an integer target, return true if target is in nums, or false if it is not in nums. |
| 10 | + * You must decrease the overall operation steps as much as possible. |
| 11 | + * <p> |
| 12 | + * Example 1: |
| 13 | + * Input: nums = [2,5,6,0,0,1,2], target = 0 |
| 14 | + * Output: true |
| 15 | + * <p> |
| 16 | + * Example 2: |
| 17 | + * Input: nums = [2,5,6,0,0,1,2], target = 3 |
| 18 | + * Output: false |
| 19 | + * <p> |
| 20 | + * Constraints: |
| 21 | + * 1 <= nums.length <= 5000 |
| 22 | + * -104 <= nums[i] <= 104 |
| 23 | + * nums is guaranteed to be rotated at some pivot. |
| 24 | + * -104 <= target <= 104 |
| 25 | + * Follow up: This problem is similar to Search in Rotated Sorted Array, but nums may contain duplicates. Would this affect the runtime complexity? How and why? |
| 26 | + */ |
3 | 27 | public class _81 {
|
4 |
| - |
5 | 28 | public static class Solution1 {
|
6 | 29 | public boolean search(int[] nums, int target) {
|
7 |
| - int start = 0; |
8 |
| - int end = nums.length - 1; |
| 30 | + int left = 0; |
| 31 | + int right = nums.length - 1; |
9 | 32 |
|
10 |
| - //check each num so we will check start == end |
| 33 | + //check each num so we will check left == right |
11 | 34 | //We always get a sorted part and a half part
|
12 | 35 | //we can check sorted part to decide where to go next
|
13 |
| - while (start <= end) { |
14 |
| - int mid = start + (end - start) / 2; |
| 36 | + while (left <= right) { |
| 37 | + int mid = left + (right - left) / 2; |
15 | 38 | if (nums[mid] == target) {
|
16 | 39 | return true;
|
17 | 40 | }
|
18 | 41 |
|
19 |
| - //if left part is sorted |
20 |
| - if (nums[start] < nums[mid]) { |
21 |
| - if (target < nums[start] || target > nums[mid]) { |
| 42 | + if (nums[left] < nums[mid]) { |
| 43 | + //if left part is sorted |
| 44 | + if (target < nums[left] || target > nums[mid]) { |
22 | 45 | //target is in rotated part
|
23 |
| - start = mid + 1; |
| 46 | + left = mid + 1; |
24 | 47 | } else {
|
25 |
| - end = mid - 1; |
| 48 | + right = mid - 1; |
26 | 49 | }
|
27 |
| - } else if (nums[start] > nums[mid]) { |
28 |
| - //right part is rotated |
29 |
| - |
30 |
| - //target is in rotated part |
31 |
| - if (target < nums[mid] || target > nums[end]) { |
32 |
| - end = mid - 1; |
| 50 | + } else if (nums[left] > nums[mid]) { |
| 51 | + //right part is sorted |
| 52 | + if (target < nums[mid] || target > nums[right]) { |
| 53 | + //target is in rotated part |
| 54 | + right = mid - 1; |
33 | 55 | } else {
|
34 |
| - start = mid + 1; |
| 56 | + left = mid + 1; |
35 | 57 | }
|
36 | 58 | } else {
|
37 |
| - //duplicates, we know nums[mid] != target, so nums[start] != target |
| 59 | + //duplicates, we know nums[mid] != target, so nums[left] != target |
38 | 60 | //based on current information, we can only move left pointer to skip one cell
|
39 | 61 | //thus in the worst case, we would have target: 2, and array like 11111111, then
|
40 | 62 | //the running time would be O(n)
|
41 |
| - start++; |
| 63 | + left++; |
42 | 64 | }
|
43 | 65 | }
|
44 | 66 | return false;
|
|
0 commit comments