Update binary tree (JS).
This commit is contained in:
parent
ee6842dc9c
commit
22314be33c
4
.github/pull_request_template.md
vendored
4
.github/pull_request_template.md
vendored
@ -1,6 +1,4 @@
|
|||||||
> Tip: If this PR is not related to the coding or code translation, please ignore the checklist.
|
If this PR is related to coding or code translation, please fill out the checklist.
|
||||||
|
|
||||||
### Checklist
|
|
||||||
|
|
||||||
- [ ] I've tested the code and ensured the outputs are the same as the outputs of reference codes.
|
- [ ] I've tested the code and ensured the outputs are the same as the outputs of reference codes.
|
||||||
- [ ] I've checked the codes (formatting, comments, indentation, file header, etc) carefully.
|
- [ ] I've checked the codes (formatting, comments, indentation, file header, etc) carefully.
|
||||||
|
@ -10,13 +10,13 @@ function binarySearch(nums, target) {
|
|||||||
let i = 0, j = nums.length - 1;
|
let i = 0, j = nums.length - 1;
|
||||||
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
||||||
while (i <= j) {
|
while (i <= j) {
|
||||||
let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
|
let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
|
||||||
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
|
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
|
||||||
i = m + 1;
|
i = m + 1;
|
||||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
|
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
|
||||||
j = m - 1;
|
j = m - 1;
|
||||||
else
|
else
|
||||||
return m; // 找到目标元素,返回其索引
|
return m; // 找到目标元素,返回其索引
|
||||||
}
|
}
|
||||||
// 未找到目标元素,返回 -1
|
// 未找到目标元素,返回 -1
|
||||||
return -1;
|
return -1;
|
||||||
@ -28,12 +28,12 @@ function binarySearch1(nums, target) {
|
|||||||
let i = 0, j = nums.length;
|
let i = 0, j = nums.length;
|
||||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||||
while (i < j) {
|
while (i < j) {
|
||||||
let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
|
let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
|
||||||
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
|
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
|
||||||
i = m + 1;
|
i = m + 1;
|
||||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
|
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
|
||||||
j = m;
|
j = m;
|
||||||
else // 找到目标元素,返回其索引
|
else // 找到目标元素,返回其索引
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
// 未找到目标元素,返回 -1
|
// 未找到目标元素,返回 -1
|
||||||
@ -50,4 +50,4 @@ console.log("目标元素 6 的索引 = " + index);
|
|||||||
|
|
||||||
/* 二分查找(左闭右开) */
|
/* 二分查找(左闭右开) */
|
||||||
index = binarySearch1(nums, target);
|
index = binarySearch1(nums, target);
|
||||||
console.log("目标元素 6 的索引 = " + index);
|
console.log("目标元素 6 的索引 = " + index);
|
||||||
|
@ -45,4 +45,4 @@ console.log("目标元素 3 的索引 = " + index);
|
|||||||
var linkedList = new ListNode();
|
var linkedList = new ListNode();
|
||||||
var head = linkedList.arrToLinkedList(nums);
|
var head = linkedList.arrToLinkedList(nums);
|
||||||
var node = linearSearchLinkedList(head, target);
|
var node = linearSearchLinkedList(head, target);
|
||||||
console.log("目标结点值 3 的对应结点对象为 " + node);
|
console.log("目标结点值 3 的对应结点对象为 " + node);
|
||||||
|
@ -29,31 +29,24 @@ $$
|
|||||||
首先,我们先采用“双闭区间”的表示,在数组 `nums` 中查找目标元素 `target` 的对应索引。
|
首先,我们先采用“双闭区间”的表示,在数组 `nums` 中查找目标元素 `target` 的对应索引。
|
||||||
|
|
||||||
=== "Step 1"
|
=== "Step 1"
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
=== "Step 2"
|
=== "Step 2"
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
=== "Step 3"
|
=== "Step 3"
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
=== "Step 4"
|
=== "Step 4"
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
=== "Step 5"
|
=== "Step 5"
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
=== "Step 6"
|
=== "Step 6"
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
=== "Step 7"
|
=== "Step 7"
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
二分查找“双闭区间”表示下的代码如下所示。
|
二分查找“双闭区间”表示下的代码如下所示。
|
||||||
@ -152,13 +145,13 @@ $$
|
|||||||
let i = 0, j = nums.length - 1;
|
let i = 0, j = nums.length - 1;
|
||||||
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
||||||
while (i <= j) {
|
while (i <= j) {
|
||||||
let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
|
let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
|
||||||
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
|
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
|
||||||
i = m + 1;
|
i = m + 1;
|
||||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
|
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
|
||||||
j = m - 1;
|
j = m - 1;
|
||||||
else
|
else
|
||||||
return m; // 找到目标元素,返回其索引
|
return m; // 找到目标元素,返回其索引
|
||||||
}
|
}
|
||||||
// 未找到目标元素,返回 -1
|
// 未找到目标元素,返回 -1
|
||||||
return -1;
|
return -1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user