From bb20cb9db28d4c662ee37342e33dda0ff6174187 Mon Sep 17 00:00:00 2001 From: JoseHung <1309716944@qq.com> Date: Thu, 22 Dec 2022 18:48:18 +0800 Subject: [PATCH 1/7] add linear_search in JS --- .../chapter_searching/linear_search.js | 48 +++++++++++++++++++ docs/chapter_searching/linear_search.md | 26 +++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 codes/javascript/chapter_searching/linear_search.js diff --git a/codes/javascript/chapter_searching/linear_search.js b/codes/javascript/chapter_searching/linear_search.js new file mode 100644 index 000000000..752e2d662 --- /dev/null +++ b/codes/javascript/chapter_searching/linear_search.js @@ -0,0 +1,48 @@ +/** + * File: linear-search.js + * Created Time: 2022-12-22 + * Author: JoseHung (szhong@link.cuhk.edu.hk) + */ + +const ListNode = require("../include/ListNode"); + +/* 线性查找(数组) */ +function linearSearchArray(nums, target) { + // 遍历数组 + for (let i = 0; i < nums.length; i++) { + // 找到目标元素,返回其索引 + if (nums[i] === target) { + return i; + } + } + // 未找到目标元素,返回 -1 + return -1; +} + +/* 线性查找(链表)*/ +function linearSearchLinkedList(head, target) { + // 遍历链表 + while(head) { + // 找到目标结点,返回之 + if(head.val === target) { + return head; + } + head = head.next; + } + // 未找到目标结点,返回 null + return null; +} + +/* Driver Code */ +var target = 3; + +/* 在数组中执行线性查找 */ +var nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8]; +var index = linearSearchArray(nums, target); +console.log("目标元素 3 的索引 = " + index); + +/* 在链表中执行线性查找 */ +var linkedList = new ListNode(); +var head = linkedList.arrToLinkedList(nums); +var node = linearSearchLinkedList(head, target); +console.log("目标结点值 3 的对应结点对象为 " + node); \ No newline at end of file diff --git a/docs/chapter_searching/linear_search.md b/docs/chapter_searching/linear_search.md index 8822d375c..e1a4ac142 100644 --- a/docs/chapter_searching/linear_search.md +++ b/docs/chapter_searching/linear_search.md @@ -76,6 +76,18 @@ comments: true === "JavaScript" ```js title="linear_search.js" + /* 线性查找(数组) */ + function linearSearchArray(nums, target) { + // 遍历数组 + for (let i = 0; i < nums.length; i++) { + // 找到目标元素,返回其索引 + if (nums[i] === target) { + return i; + } + } + // 未找到目标元素,返回 -1 + return -1; + } ``` @@ -167,7 +179,19 @@ comments: true === "JavaScript" ```js title="linear_search.js" - + /* 线性查找(链表)*/ + function linearSearchLinkedList(head, target) { + // 遍历链表 + while(head) { + // 找到目标结点,返回之 + if(head.val === target) { + return head; + } + head = head.next; + } + // 未找到目标结点,返回 null + return null; + } ``` === "TypeScript" From c3395be905b25c8ebd9fb1b15080f849e36c8929 Mon Sep 17 00:00:00 2001 From: JoseHung <1309716944@qq.com> Date: Thu, 22 Dec 2022 18:53:38 +0800 Subject: [PATCH 2/7] fix the error about assignment to constant --- codes/javascript/include/ListNode.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/javascript/include/ListNode.js b/codes/javascript/include/ListNode.js index 72306548c..6e226e9c3 100755 --- a/codes/javascript/include/ListNode.js +++ b/codes/javascript/include/ListNode.js @@ -22,7 +22,7 @@ class ListNode { */ arrToLinkedList(arr) { const dum = new ListNode(0); - const head = dum; + let head = dum; for (const val of arr) { head.next = new ListNode(val); head = head.next; From 14173133415e63a5e0425ae496d1985fa300b084 Mon Sep 17 00:00:00 2001 From: JoseHung <1309716944@qq.com> Date: Thu, 22 Dec 2022 21:38:08 +0800 Subject: [PATCH 3/7] add binary_search in JS --- .../chapter_searching/binary_search.js | 53 +++++++++++++++++++ docs/chapter_searching/binary_search.md | 41 ++++++++++++-- 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 codes/javascript/chapter_searching/binary_search.js diff --git a/codes/javascript/chapter_searching/binary_search.js b/codes/javascript/chapter_searching/binary_search.js new file mode 100644 index 000000000..1c4ed2a12 --- /dev/null +++ b/codes/javascript/chapter_searching/binary_search.js @@ -0,0 +1,53 @@ +/** + * File: binary_search.js + * Created Time: 2022-12-22 + * Author: JoseHung (szhong@link.cuhk.edu.hk) + */ + +/* 二分查找(双闭区间) */ +function binarySearch(nums, target) { + // 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素 + let i = 0, j = nums.length - 1; + // 循环,当搜索区间为空时跳出(当 i > j 时为空) + while (i <= j) { + let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整 + if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中 + i = m + 1; + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中 + j = m - 1; + else + return m; // 找到目标元素,返回其索引 + } + // 未找到目标元素,返回 -1 + return -1; +} + +/* 二分查找(左闭右开) */ +function binarySearch1(nums, target) { + // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 + let i = 0, j = nums.length; + // 循环,当搜索区间为空时跳出(当 i = j 时为空) + while (i < j) { + let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整 + if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中 + i = m + 1; + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中 + j = m; + else // 找到目标元素,返回其索引 + return m; + } + // 未找到目标元素,返回 -1 + return -1; +} + +/* Driver Code */ +var target = 6; +var nums = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92]; + +/* 二分查找(双闭区间) */ +var index = binarySearch(nums, target); +console.log("目标元素 6 的索引 = " + index); + +/* 二分查找(左闭右开) */ +index = binarySearch1(nums, target); +console.log("目标元素 6 的索引 = " + index); \ No newline at end of file diff --git a/docs/chapter_searching/binary_search.md b/docs/chapter_searching/binary_search.md index dd9192e09..b5eeb8f93 100644 --- a/docs/chapter_searching/binary_search.md +++ b/docs/chapter_searching/binary_search.md @@ -146,7 +146,23 @@ $$ === "JavaScript" ```js title="binary_search.js" - + /* 二分查找(双闭区间) */ + function binarySearch(nums, target) { + // 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素 + let i = 0, j = nums.length - 1; + // 循环,当搜索区间为空时跳出(当 i > j 时为空) + while (i <= j) { + let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整 + if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中 + i = m + 1; + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中 + j = m - 1; + else + return m; // 找到目标元素,返回其索引 + } + // 未找到目标元素,返回 -1 + return -1; + } ``` === "TypeScript" @@ -260,7 +276,23 @@ $$ === "JavaScript" ```js title="binary_search.js" - + /* 二分查找(左闭右开) */ + function binarySearch1(nums, target) { + // 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1 + let i = 0, j = nums.length; + // 循环,当搜索区间为空时跳出(当 i = j 时为空) + while (i < j) { + let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整 + if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中 + i = m + 1; + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中 + j = m; + else // 找到目标元素,返回其索引 + return m; + } + // 未找到目标元素,返回 -1 + return -1; + } ``` === "TypeScript" @@ -337,7 +369,10 @@ $$ === "JavaScript" ```js title="" - + // (i + j) 有可能超出 int 的取值范围 + let m = parseInt((i + j) / 2); + // 更换为此写法则不会越界 + let m = parseInt(i + (j - i) / 2); ``` === "TypeScript" From f661a3455db585dfeceb3a9806b1d4fcf8af6f9e Mon Sep 17 00:00:00 2001 From: JoseHung <1309716944@qq.com> Date: Fri, 23 Dec 2022 00:52:41 +0800 Subject: [PATCH 4/7] fix the annotation --- codes/javascript/chapter_searching/binary_search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/javascript/chapter_searching/binary_search.js b/codes/javascript/chapter_searching/binary_search.js index 1c4ed2a12..2272f823e 100644 --- a/codes/javascript/chapter_searching/binary_search.js +++ b/codes/javascript/chapter_searching/binary_search.js @@ -31,7 +31,7 @@ function binarySearch1(nums, target) { let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整 if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中 i = m + 1; - else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中 + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m] 中 j = m; else // 找到目标元素,返回其索引 return m; From 5c295bdc316d148308a9f6c785e9a958c3edf59b Mon Sep 17 00:00:00 2001 From: JoseHung <1309716944@qq.com> Date: Fri, 23 Dec 2022 00:55:08 +0800 Subject: [PATCH 5/7] fix the annotation --- codes/javascript/chapter_searching/binary_search.js | 2 +- docs/chapter_searching/binary_search.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/javascript/chapter_searching/binary_search.js b/codes/javascript/chapter_searching/binary_search.js index 2272f823e..8da584a1c 100644 --- a/codes/javascript/chapter_searching/binary_search.js +++ b/codes/javascript/chapter_searching/binary_search.js @@ -31,7 +31,7 @@ function binarySearch1(nums, target) { let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整 if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中 i = m + 1; - else if (nums[m] > target) // 此情况说明 target 在区间 [i, m] 中 + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中 j = m; else // 找到目标元素,返回其索引 return m; diff --git a/docs/chapter_searching/binary_search.md b/docs/chapter_searching/binary_search.md index b5eeb8f93..adb3b209f 100644 --- a/docs/chapter_searching/binary_search.md +++ b/docs/chapter_searching/binary_search.md @@ -285,7 +285,7 @@ $$ let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整 if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中 i = m + 1; - else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中 + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中 j = m; else // 找到目标元素,返回其索引 return m; From c4c10afdb149be30390f3d5f326651c7b534064b Mon Sep 17 00:00:00 2001 From: JoseHung <1309716944@qq.com> Date: Fri, 23 Dec 2022 01:10:53 +0800 Subject: [PATCH 6/7] fix the annotation of large number problem --- docs/chapter_searching/binary_search.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/chapter_searching/binary_search.md b/docs/chapter_searching/binary_search.md index adb3b209f..081445ab9 100644 --- a/docs/chapter_searching/binary_search.md +++ b/docs/chapter_searching/binary_search.md @@ -369,10 +369,8 @@ $$ === "JavaScript" ```js title="" - // (i + j) 有可能超出 int 的取值范围 - let m = parseInt((i + j) / 2); - // 更换为此写法则不会越界 - let m = parseInt(i + (j - i) / 2); + // JavaScript 中的数字足够大(最大数值为 1.7976931348623157e+308 ) + // 因此无需考虑大数越界问题 ``` === "TypeScript" From 539aeefc755f9624a05170553b381530e60008d8 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Fri, 23 Dec 2022 01:17:37 +0800 Subject: [PATCH 7/7] Update binary_search.md --- docs/chapter_searching/binary_search.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/chapter_searching/binary_search.md b/docs/chapter_searching/binary_search.md index 081445ab9..abb91455b 100644 --- a/docs/chapter_searching/binary_search.md +++ b/docs/chapter_searching/binary_search.md @@ -282,12 +282,12 @@ $$ let i = 0, j = nums.length; // 循环,当搜索区间为空时跳出(当 i = j 时为空) while (i < j) { - let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整 - if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中 + let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整 + if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中 i = m + 1; - else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中 + else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中 j = m; - else // 找到目标元素,返回其索引 + else // 找到目标元素,返回其索引 return m; } // 未找到目标元素,返回 -1 @@ -369,8 +369,10 @@ $$ === "JavaScript" ```js title="" - // JavaScript 中的数字足够大(最大数值为 1.7976931348623157e+308 ) - // 因此无需考虑大数越界问题 + // (i + j) 有可能超出 int 的取值范围 + let m = parseInt((i + j) / 2); + // 更换为此写法则不会越界 + let m = parseInt(i + (j - i) / 2); ``` === "TypeScript"