Topic description
给定n
个元素有序的(升序) nums
target
,写一个函数搜索nums
in target
, if the target value exists, return the subscript, otherwise return -1
.
Example 1:
输入: nums = [-1,0,3,5,9,12], target = 9
输出: 4
解释: 9 出现在 nums 中并且下标为 4
Example 2:
输入: nums = [-1,0,3,5,9,12], target = 2
输出: -1
解释: 2 不存在 nums 中因此返回 -1
Leetcode original title address: https://leetcode.cn/problems/binary-search/
Solution 1, use the array native method indexOf
var search = function (nums, target) {
return nums.indexOf(target)
};
The native method of array is really convenient to use. However, sometimes it is necessary to expand some ways to deal with problems, which will be of great benefit when encountering requirements in the future.
Solution 2, loop all judgments whether they are equal to the target value
var search = function (nums, target) {
let whichIndex = -1
for (let i = 0; i < nums.length; i++) {
if (nums[i] == target) {
whichIndex = i
return whichIndex
}
}
return whichIndex
};
Assuming that the value we are looking for is exactly at the last position, then we have to go through the entire loop to find the value we are looking for. So the time complexity is O(n)
. In order to further improve the efficiency and reduce the time, the dichotomous problem-solving method came into being.
Solution three, dichotomous problem solving
The so-called dichotomy can be simply understood as:
- Specify the start position, end position, and then continue to narrow its range to finally find the value we want to find;
- Due to the reduced range of half and half of the dichotomy, its time complexity is
O(\log n)
Also note:
- Dichotomy applies to ordered arrays, i.e. from small to large or from large to small
- The readability of the dichotomy with the while loop will be more intuitive and clear
In the work, the for loop is used more times, and the while loop is not particularly large, so in order to better understand the dichotomy, let's briefly review the while loop knowledge
while loop review
// 当符合condition条件的时候,会继续不断循环执行花括号里面的代码
// 所以当达到某种情况下的时候,需要让condition条件变为false,这样的话,才会结束循环
// 要不然就会死循环哦
while (condition) {
// .....
}
// eg:使用while循环打印0~10
let num = 0
while (num <= 10) { // 翻译:当num的值小于等于10的时候,就不断循环
console.log(num);
num++
}
Difference between while and for loop
-
固定数组、固定长度、固定执行次数一般使用for循环
-
做边界区分、不知道要循环多少次一般使用while循环
-
while比for功能更加强大
Dichotomy code
var search = function (arr, num) {
let firstIndex = 0
let lastIndex = arr.length - 1
// 注意firstIndex是要小于等于lastIndex值的,因为区间不断缩小最终重合
while (firstIndex <= lastIndex) {
// console.log('firstIndex', firstIndex, 'lastIndex', lastIndex); // 看打印结果,有助于更好理解
let middleIndex = Math.floor((firstIndex + lastIndex) / 2) // 数组长度为偶数时要向下取整,所以干脆都向下取整呗
if (arr[middleIndex] == num) { // 若恰好中间索引数正好是我们所要找的那个数,返回索引即可
return middleIndex
} else if (arr[middleIndex] > num) { // 若中间数比要找的数大,说明要找到数在左边,那么开始区间不用动,结束区间更改为中间数左边一个位置即可
lastIndex = middleIndex - 1
} else if (arr[middleIndex] < num) { // 同理...
firstIndex = middleIndex + 1
}
}
return -1 // 找不到的话,返回-1
};
Here you can change while (firstIndex <= lastIndex) {......}
to while (true) {......}
to see the print result for a better understanding. Although an infinite loop
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。