Polish the content

Polish the chapter preface, introduction and complexity anlysis
This commit is contained in:
krahets
2023-08-08 23:16:33 +08:00
parent 9ed16db68e
commit 932d14644d
26 changed files with 215 additions and 182 deletions

View File

@@ -71,7 +71,7 @@ int bubbleSort(int *nums, int n) {
int exponential(int n) {
int count = 0;
int bas = 1;
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++) {
for (int j = 0; j < bas; j++) {
count++;

View File

@@ -67,7 +67,7 @@ int bubbleSort(vector<int> &nums) {
/* 指数阶(循环实现) */
int exponential(int n) {
int count = 0, base = 1;
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++) {
for (int j = 0; j < base; j++) {
count++;

View File

@@ -98,7 +98,7 @@ public class time_complexity {
/* 指数阶(循环实现) */
static int exponential(int n) {
int count = 0, bas = 1;
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++) {
for (int j = 0; j < bas; j++) {
count++;

View File

@@ -67,7 +67,7 @@ int bubbleSort(List<int> nums) {
/* 指数阶(循环实现) */
int exponential(int n) {
int count = 0, base = 1;
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (var i = 0; i < n; i++) {
for (var j = 0; j < base; j++) {
count++;

View File

@@ -67,7 +67,7 @@ func bubbleSort(nums []int) int {
/* 指数阶(循环实现)*/
func exponential(n int) int {
count, base := 0, 1
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for i := 0; i < n; i++ {
for j := 0; j < base; j++ {
count++

View File

@@ -11,42 +11,42 @@ import (
func TestArrayHashMap(t *testing.T) {
/* 初始化哈希表 */
mapp := newArrayHashMap()
hmap := newArrayHashMap()
/* 添加操作 */
// 在哈希表中添加键值对 (key, value)
mapp.put(12836, "小哈")
mapp.put(15937, "小啰")
mapp.put(16750, "小算")
mapp.put(13276, "小法")
mapp.put(10583, "小鸭")
hmap.put(12836, "小哈")
hmap.put(15937, "小啰")
hmap.put(16750, "小算")
hmap.put(13276, "小法")
hmap.put(10583, "小鸭")
fmt.Println("\n添加完成后哈希表为\nKey -> Value")
mapp.print()
hmap.print()
/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
name := mapp.get(15937)
name := hmap.get(15937)
fmt.Println("\n输入学号 15937 ,查询到姓名 " + name)
/* 删除操作 */
// 在哈希表中删除键值对 (key, value)
mapp.remove(10583)
hmap.remove(10583)
fmt.Println("\n删除 10583 后,哈希表为\nKey -> Value")
mapp.print()
hmap.print()
/* 遍历哈希表 */
fmt.Println("\n遍历键值对 Key->Value")
for _, kv := range mapp.pairSet() {
for _, kv := range hmap.pairSet() {
fmt.Println(kv.key, " -> ", kv.val)
}
fmt.Println("\n单独遍历键 Key")
for _, key := range mapp.keySet() {
for _, key := range hmap.keySet() {
fmt.Println(key)
}
fmt.Println("\n单独遍历值 Value")
for _, val := range mapp.valueSet() {
for _, val := range hmap.valueSet() {
fmt.Println(val)
}
}

View File

@@ -68,7 +68,7 @@ public class time_complexity {
/* 指数阶(循环实现) */
static int exponential(int n) {
int count = 0, base = 1;
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (int i = 0; i < n; i++) {
for (int j = 0; j < base; j++) {
count++;

View File

@@ -64,7 +64,7 @@ function bubbleSort(nums) {
function exponential(n) {
let count = 0,
base = 1;
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (let i = 0; i < n; i++) {
for (let j = 0; j < base; j++) {
count++;

View File

@@ -35,9 +35,9 @@ def linear(n: int):
# 长度为 n 的列表占用 O(n) 空间
nums = [0] * n
# 长度为 n 的哈希表占用 O(n) 空间
mapp = dict[int, str]()
hmap = dict[int, str]()
for i in range(n):
mapp[i] = str(i)
hmap[i] = str(i)
def linear_recur(n: int):

View File

@@ -61,7 +61,7 @@ def exponential(n: int) -> int:
"""指数阶(循环实现)"""
count = 0
base = 1
# cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
# 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for _ in range(n):
for _ in range(base):
count += 1

View File

@@ -80,38 +80,38 @@ class ArrayHashMap:
"""Driver Code"""
if __name__ == "__main__":
# 初始化哈希表
mapp = ArrayHashMap()
hmap = ArrayHashMap()
# 添加操作
# 在哈希表中添加键值对 (key, value)
mapp.put(12836, "小哈")
mapp.put(15937, "小啰")
mapp.put(16750, "小算")
mapp.put(13276, "小法")
mapp.put(10583, "小鸭")
hmap.put(12836, "小哈")
hmap.put(15937, "小啰")
hmap.put(16750, "小算")
hmap.put(13276, "小法")
hmap.put(10583, "小鸭")
print("\n添加完成后,哈希表为\nKey -> Value")
mapp.print()
hmap.print()
# 查询操作
# 向哈希表输入键 key ,得到值 value
name = mapp.get(15937)
name = hmap.get(15937)
print("\n输入学号 15937 ,查询到姓名 " + name)
# 删除操作
# 在哈希表中删除键值对 (key, value)
mapp.remove(10583)
hmap.remove(10583)
print("\n删除 10583 后,哈希表为\nKey -> Value")
mapp.print()
hmap.print()
# 遍历哈希表
print("\n遍历键值对 Key->Value")
for pair in mapp.entry_set():
for pair in hmap.entry_set():
print(pair.key, "->", pair.val)
print("\n单独遍历键 Key")
for key in mapp.key_set():
for key in hmap.key_set():
print(key)
print("\n单独遍历值 Value")
for val in mapp.value_set():
for val in hmap.value_set():
print(val)

View File

@@ -12,38 +12,38 @@ from modules import *
"""Driver Code"""
if __name__ == "__main__":
# 初始化哈希表
mapp = dict[int, str]()
hmap = dict[int, str]()
# 添加操作
# 在哈希表中添加键值对 (key, value)
mapp[12836] = "小哈"
mapp[15937] = "小啰"
mapp[16750] = "小算"
mapp[13276] = "小法"
mapp[10583] = "小鸭"
hmap[12836] = "小哈"
hmap[15937] = "小啰"
hmap[16750] = "小算"
hmap[13276] = "小法"
hmap[10583] = "小鸭"
print("\n添加完成后,哈希表为\nKey -> Value")
print_dict(mapp)
print_dict(hmap)
# 查询操作
# 向哈希表输入键 key ,得到值 value
name: str = mapp[15937]
name: str = hmap[15937]
print("\n输入学号 15937 ,查询到姓名 " + name)
# 删除操作
# 在哈希表中删除键值对 (key, value)
mapp.pop(10583)
hmap.pop(10583)
print("\n删除 10583 后,哈希表为\nKey -> Value")
print_dict(mapp)
print_dict(hmap)
# 遍历哈希表
print("\n遍历键值对 Key->Value")
for key, value in mapp.items():
for key, value in hmap.items():
print(key, "->", value)
print("\n单独遍历键 Key")
for key in mapp.keys():
for key in hmap.keys():
print(key)
print("\n单独遍历值 Value")
for val in mapp.values():
for val in hmap.values():
print(val)

View File

@@ -10,20 +10,20 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from modules import *
def hashing_search_array(mapp: dict[int, int], target: int) -> int:
def hashing_search_array(hmap: dict[int, int], target: int) -> int:
"""哈希查找(数组)"""
# 哈希表的 key: 目标元素value: 索引
# 若哈希表中无此 key ,返回 -1
return mapp.get(target, -1)
return hmap.get(target, -1)
def hashing_search_linkedlist(
mapp: dict[int, ListNode], target: int
hmap: dict[int, ListNode], target: int
) -> ListNode | None:
"""哈希查找(链表)"""
# 哈希表的 key: 目标元素value: 节点对象
# 若哈希表中无此 key ,返回 None
return mapp.get(target, None)
return hmap.get(target, None)
"""Driver Code"""

View File

@@ -67,9 +67,9 @@ def print_tree(
print_tree(root.left, trunk, False)
def print_dict(mapp: dict):
def print_dict(hmap: dict):
"""Print a dict"""
for key, value in mapp.items():
for key, value in hmap.items():
print(key, "->", value)

View File

@@ -69,7 +69,7 @@ fn bubble_sort(nums: &mut [i32]) -> i32 {
fn exponential(n: i32) -> i32 {
let mut count = 0;
let mut base = 1;
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for _ in 0..n {
for _ in 0..base {
count += 1

View File

@@ -68,7 +68,7 @@ func bubbleSort(nums: inout [Int]) -> Int {
func exponential(n: Int) -> Int {
var count = 0
var base = 1
// cell 1, 2, 4, 8, ..., 2^(n-1)
// 1, 2, 4, 8, ..., 2^(n-1)
for _ in 0 ..< n {
for _ in 0 ..< base {
count += 1

View File

@@ -64,7 +64,7 @@ function bubbleSort(nums: number[]): number {
function exponential(n: number): number {
let count = 0,
base = 1;
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
for (let i = 0; i < n; i++) {
for (let j = 0; j < base; j++) {
count++;

View File

@@ -76,7 +76,7 @@ fn exponential(n: i32) i32 {
var count: i32 = 0;
var bas: i32 = 1;
var i: i32 = 0;
// cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
// 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1)
while (i < n) : (i += 1) {
var j: i32 = 0;
while (j < bas) : (j += 1) {