Skip to content

Commit d5df6fd

Browse files
committed
leetcode
1 parent 68858c6 commit d5df6fd

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
3+
-* 81. Search in Rotated Sorted Array II *-
4+
5+
There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values).
6+
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 [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].
8+
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+
11+
You must decrease the overall operation steps as much as possible.
12+
13+
14+
15+
Example 1:
16+
17+
Input: nums = [2,5,6,0,0,1,2], target = 0
18+
Output: true
19+
Example 2:
20+
21+
Input: nums = [2,5,6,0,0,1,2], target = 3
22+
Output: false
23+
24+
25+
Constraints:
26+
27+
1 <= nums.length <= 5000
28+
-104 <= nums[i] <= 104
29+
nums is guaranteed to be rotated at some pivot.
30+
-104 <= target <= 104
31+
32+
33+
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?
34+
35+
36+
*/
37+
38+
39+
40+
class Solution {
41+
bool search(List<int> nums, int target) {
42+
for (int i = 0; i < nums.length; i++) {
43+
if ((nums[i] ^ target) == 0) {
44+
return true;
45+
}
46+
}
47+
48+
return false;
49+
}
50+
}
51+
52+
class A {
53+
bool search(List<int> nums, int target) {
54+
int n = nums.length;
55+
if (target > nums[n - 1]) {
56+
int i = 0;
57+
do {
58+
if (nums[i] == target) return true;
59+
i++;
60+
} while (i < n && nums[i] >= nums[i - 1]);
61+
} else {
62+
int i = n - 1;
63+
do {
64+
if (nums[i] == target) return true;
65+
i--;
66+
} while (i >= 0 && nums[i] <= nums[i + 1]);
67+
}
68+
return false;
69+
}
70+
}
71+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"sort"
5+
)
6+
7+
func search(nums []int, target int) bool {
8+
// Order the array in order to search
9+
ordered := append([]int{}, nums...)
10+
sort.Ints(ordered)
11+
12+
init := 0
13+
end := len(ordered) - 1
14+
15+
// To memoize the computed values
16+
memo := make(map[int]int)
17+
18+
for {
19+
// Change the middle index based on the last init/end index value
20+
mid := (end + init) / 2
21+
22+
// Memoize the computed indexes to avoid infinite loop
23+
// If they were computed before, it means the value does not exist in the array
24+
// If the value doesn't exist, break the loop and return false
25+
if _, ok := memo[mid]; ok {
26+
return false
27+
} else {
28+
memo[mid] = 0
29+
}
30+
31+
// Regular binary search
32+
if ordered[mid] == target {
33+
return true
34+
} else if ordered[mid] < target {
35+
init = mid + 1
36+
} else {
37+
end = mid - 1
38+
}
39+
}
40+
}
41+
42+
43+
func searchONE(nums []int, target int) bool {
44+
for _, num := range nums {
45+
if num == target {
46+
return true
47+
}
48+
}
49+
return false
50+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Doc
2+
3+
## [GO](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/solutions/3890111/go-xor-memoization-simple-fast-and-easy-with-explanation/)
4+
5+
## [DART](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/solutions/3890102/3-solutions-simple-fast-and-easy-with-explanation/)

0 commit comments

Comments
 (0)