mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
Merge branch 'krahets:master' into typescript
This commit is contained in:
53
codes/javascript/chapter_searching/binary_search.js
Normal file
53
codes/javascript/chapter_searching/binary_search.js
Normal file
@@ -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) 中
|
||||
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);
|
||||
48
codes/javascript/chapter_searching/linear_search.js
Normal file
48
codes/javascript/chapter_searching/linear_search.js
Normal file
@@ -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);
|
||||
@@ -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;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: array.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: linked_list.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: list.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: my_list.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: leetcode_two_sum.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: space_complexity.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: time_complexity.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: worst_best_time_complexity.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: binary_search.py
|
||||
Created Time: 2022-11-26
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: hashing_search.py
|
||||
Created Time: 2022-11-26
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: linear_search.py
|
||||
Created Time: 2022-11-26
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: bubble_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: insertion_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: merge_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: quick_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: timi (xisunyy@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: array_queue.py
|
||||
Created Time: 2022-12-01
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import os.path as osp
|
||||
import sys
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: array_stack.py
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: deque.py
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import os.path as osp
|
||||
import sys
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: linkedlist_queue.py
|
||||
Created Time: 2022-12-01
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import os.path as osp
|
||||
import sys
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: linkedlist_stack.py
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: queue.py
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import os.path as osp
|
||||
import sys
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: stack.py
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: binary_search_tree.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: binary_tree.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: binary_tree_bfs.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: binary_tree_dfs.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: binary_tree.py
|
||||
Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import collections
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: linked_list.py
|
||||
Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
class ListNode:
|
||||
"""Definition for a singly-linked list node
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
'''
|
||||
"""
|
||||
File: print_util.py
|
||||
Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com), msk397 (machangxinq@gmail.com)
|
||||
'''
|
||||
"""
|
||||
|
||||
import copy
|
||||
import queue
|
||||
|
||||
Reference in New Issue
Block a user