Skip to content

Commit 5be49c6

Browse files
committed
searchs, guessNumber
1 parent 376b2b4 commit 5be49c6

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
| 0030 | [findSubstring](./code/0030_findSubstring) | ⭐⭐⭐ | Hash Table, Two Pointers, String | |
2121
| 0031 | [nextPermutation](./code/0031_nextPermutation) | ⭐⭐ | Array | |
2222
| 0032 | [longestValidParentheses](./code/0032_longestValidParentheses) | ⭐⭐⭐ | DP, Stack | |
23+
| 0033 | [search](./code/0033_search) | ⭐⭐ | Array, Binary Search | 1️⃣ |
2324
| 0034 | [searchRange](./code/0034_searchRange) | ⭐⭐ | Array, Binary Search | |
2425
| 0035 | [searchInsert](./code/0035_searchInsert) || Array, Binary Search | |
2526
| 0039 | [combinationSum](./code/0039_combinationSum) | ⭐⭐ | Array, Backtracking | |
@@ -56,6 +57,7 @@
5657
| 0328 | [oddEvenList](./code/0328_oddEvenList) | ⭐⭐ | Linked List | |
5758
| 0347 | [topKFrequent](./code/0347_topKFrequent) | ⭐⭐ | Hash Table, Heap | 1️⃣✅ |
5859
| 0372 | [getSum](./code/0372_getSum) || Bit | |
60+
| 0374 | [getSum](./code/0374_guessNumber) || Binary Search | 1️⃣✅ |
5961
| 0376 | [wiggleMaxLength](./code/0376_wiggleMaxLength) | ⭐⭐ | Greedy, DP | |
6062
| 0387 | [firstUniqChar](./code/0387_firstUniqChar) || Hash Table, Array | |
6163
| 0389 | [findTheDifference](./code/0389_findTheDifference) || Hash Table, Bit | |
@@ -72,6 +74,7 @@
7274
| 0605 | [canPlaceFlowers](./code/0605_canPlaceFlowers) || Greedy | |
7375
| 0628 | [maximumProduct](./code/0628_maximumProduct) || Array | |
7476
| 0674 | [findLengthOfLCIS](./code/0674_findLengthOfLCIS) || Array | |
77+
| 0704 | [search](./code/0704_search) || Binary Search | 1️⃣✅ |
7578
| 0724 | [pivotIndex](./code/0724_pivotIndex) || Array | 1️⃣✅📊 |
7679
| 0738 | [monotoneIncreasingDigits](./code/0738_monotoneIncreasingDigits) | ⭐⭐ | Greedy | |
7780
| 0766 | [isToeplitzMatrix](./code/0766_isToeplitzMatrix) || Array | 1️⃣✅ |
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
var search = function (nums, target) {
7+
let l = 0, r = nums.length - 1;
8+
9+
while (l <= r) {
10+
const mid = l + Math.floor((r - l) >> 1);
11+
if (nums[mid] === target) {
12+
return mid;
13+
}
14+
if (nums[0] <= nums[mid]) {
15+
if (nums[0] <= target && target < nums[mid]) {
16+
r = mid - 1;
17+
} else {
18+
l = mid + 1;
19+
}
20+
} else {
21+
if (nums[mid] < target && target <= nums[nums.length - 1]) {
22+
l = mid + 1;
23+
} else {
24+
r = mid - 1;
25+
}
26+
}
27+
}
28+
29+
return -1;
30+
};
31+
32+
// 时间复杂度 O(logN)
33+
// 空间复杂度 O(1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Forward declaration of guess API.
3+
* @param {number} num your guess
4+
* @return -1 if num is lower than the guess number
5+
* 1 if num is higher than the guess number
6+
* otherwise return 0
7+
* var guess = function(num) {}
8+
*/
9+
10+
/**
11+
* @param {number} n
12+
* @return {number}
13+
*/
14+
var guessNumber = function (n) {
15+
let l = 0, r = n;
16+
17+
while (l <= r) {
18+
const mid = l + Math.floor((r - l) >> 1);
19+
20+
if (guess(mid) === 0) {
21+
return mid;
22+
}
23+
if (guess(mid) === 1) {
24+
l = mid + 1;
25+
} else {
26+
r = mid - 1;
27+
}
28+
}
29+
};
30+
31+
// 时间复杂度 O(logN)
32+
// 空间复杂度 O(1)
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
var search = function (nums, target) {
7+
let l = 0, r = nums.length - 1;
8+
while (l <= r) {
9+
const mid = l + Math.floor((r - l) >> 1);
10+
if (nums[mid] === target) {
11+
return mid;
12+
}
13+
if (nums[mid] < target) {
14+
l = mid + 1;
15+
} else {
16+
r = mid - 1;
17+
}
18+
}
19+
20+
return -1;
21+
};
22+
23+
// 时间复杂度 O(logN)
24+
// 空间复杂度 O(1)

0 commit comments

Comments
 (0)