mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-25 21:23:21 +08:00
Fix compile errors in doc code snippets (#2033)
Seventeen snippet blocks in codes/snippets did not compile or did something other than
what their comments say. Each fix is the smallest change that makes the snippet valid,
and follows the matching demo code where one exists:
- space_complexity: Java and C `if` bodies get braces; C drops the initializer on a
variable-length array; Go assigns instead of shadowing `nums`; Rust renames the
`loop` function (a keyword) to `for_loop`; Dart makes `Node.next` nullable.
- time_complexity: Ruby `.each do { ... }` becomes `.each { ... }`.
- basic_data_types: Ruby `ListNode(0)` becomes `ListNode.new(0)`.
- heap: C++ assigns `minHeap` instead of declaring it twice.
- hash_map: Dart reads into `String?`; Rust uses key 13276 like the other languages.
- list: Rust `nums` is `mut`; Kotlin removes index 3 with `removeAt(3)`.
- linked_list: C# node fields are public and nullable; a stray `\` after a Dart
comment is removed.
- backtracking_algorithm: Python `list[Choice]`/`list[State]`, C++ `Choice *choice`,
Dart `List<Choice> choices`.
All six editions change identically; zh-hant is regenerated by OpenCC. Comments are
unchanged.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -28,8 +28,8 @@ class ListNode {
|
||||
```csharp title=""
|
||||
/* 链表节点类 */
|
||||
class ListNode(int x) { //构造函数
|
||||
int val = x; // 节点值
|
||||
ListNode? next; // 指向下一节点的引用
|
||||
public int val = x; // 节点值
|
||||
public ListNode? next; // 指向下一节点的引用
|
||||
}
|
||||
```
|
||||
|
||||
@@ -264,7 +264,7 @@ n3.next = n4;
|
||||
```
|
||||
|
||||
```dart title="linked_list.dart"
|
||||
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */\
|
||||
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
||||
// 初始化各个节点
|
||||
ListNode n0 = ListNode(1);
|
||||
ListNode n1 = ListNode(3);
|
||||
@@ -375,9 +375,9 @@ class ListNode {
|
||||
```csharp title=""
|
||||
/* 双向链表节点类 */
|
||||
class ListNode(int x) { // 构造函数
|
||||
int val = x; // 节点值
|
||||
ListNode next; // 指向后继节点的引用
|
||||
ListNode prev; // 指向前驱节点的引用
|
||||
public int val = x; // 节点值
|
||||
public ListNode? next; // 指向后继节点的引用
|
||||
public ListNode? prev; // 指向前驱节点的引用
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ List<int> nums = [1, 3, 2, 5, 4];
|
||||
// 无初始值
|
||||
let nums1: Vec<i32> = Vec::new();
|
||||
// 有初始值
|
||||
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
let mut nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
```
|
||||
|
||||
```c title="list.c"
|
||||
@@ -404,10 +404,10 @@ nums.add(5);
|
||||
nums.add(4);
|
||||
|
||||
/* 在中间插入元素 */
|
||||
nums.add(3, 6); // 在索引 3 处插入数字 6
|
||||
nums.add(3, 6); // 在索引 3 处插入数字 6
|
||||
|
||||
/* 删除元素 */
|
||||
nums.remove(3); // 删除索引 3 处的元素
|
||||
nums.removeAt(3); // 删除索引 3 处的元素
|
||||
```
|
||||
|
||||
```ruby title="list.rb"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<!-- [snippet]{backtrack_framework} -->
|
||||
```python title=""
|
||||
def backtrack(state: State, choices: list[choice], res: list[state]):
|
||||
def backtrack(state: State, choices: list[Choice], res: list[State]):
|
||||
"""回溯算法框架"""
|
||||
# 判断是否为解
|
||||
if is_solution(state):
|
||||
@@ -30,7 +30,7 @@ void backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (Choice choice : choices) {
|
||||
for (Choice *choice : choices) {
|
||||
// 剪枝:判断选择是否合法
|
||||
if (isValid(state, choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
@@ -189,7 +189,7 @@ function backtrack(state: State, choices: Choice[], res: State[]): void {
|
||||
|
||||
```dart title=""
|
||||
/* 回溯算法框架 */
|
||||
void backtrack(State state, List<Choice>, List<State> res) {
|
||||
void backtrack(State state, List<Choice> choices, List<State> res) {
|
||||
// 判断是否为解
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
|
||||
@@ -196,7 +196,7 @@ function algorithm(n: number): number { // 输入数据
|
||||
/* 类 */
|
||||
class Node {
|
||||
int val;
|
||||
Node next;
|
||||
Node? next;
|
||||
Node(this.val, [this.next]);
|
||||
}
|
||||
|
||||
@@ -332,8 +332,9 @@ void algorithm(int n) {
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
int[] b = new int[10000]; // O(1)
|
||||
if (n > 10)
|
||||
if (n > 10) {
|
||||
int[] nums = new int[n]; // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -353,7 +354,7 @@ func algorithm(n int) {
|
||||
b := make([]int, 10000) // O(1)
|
||||
var nums []int
|
||||
if n > 10 {
|
||||
nums := make([]int, n) // O(n)
|
||||
nums = make([]int, n) // O(n)
|
||||
}
|
||||
fmt.Println(a, b, nums)
|
||||
}
|
||||
@@ -413,8 +414,9 @@ fn algorithm(n: i32) {
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
int b[10000]; // O(1)
|
||||
if (n > 10)
|
||||
int nums[n] = {0}; // O(n)
|
||||
if (n > 10) {
|
||||
int nums[n]; // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -613,7 +615,7 @@ fn function() -> i32 {
|
||||
return 0;
|
||||
}
|
||||
/* 循环的空间复杂度为 O(1) */
|
||||
fn loop(n: i32) {
|
||||
fn for_loop(n: i32) {
|
||||
for i in 0..n {
|
||||
function();
|
||||
}
|
||||
|
||||
@@ -774,10 +774,10 @@ def algorithm(n)
|
||||
a = 1 # +0(技巧 1)
|
||||
a = a + n # +0(技巧 1)
|
||||
# +n(技巧 2)
|
||||
(0...(5 * n + 1)).each do { puts 0 }
|
||||
(0...(5 * n + 1)).each { puts 0 }
|
||||
# +n*n(技巧 3)
|
||||
(0...(2 * n)).each do
|
||||
(0...(n + 1)).each do { puts 0 }
|
||||
(0...(n + 1)).each { puts 0 }
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
@@ -96,7 +96,7 @@ val bools = BooleanArray(5)
|
||||
|
||||
```ruby title=""
|
||||
# Ruby 的列表可以自由存储各种基本数据类型和对象引用
|
||||
data = [0, 0.0, 'a', false, ListNode(0)]
|
||||
data = [0, 0.0, 'a', false, ListNode.new(0)]
|
||||
```
|
||||
|
||||
<!-- [tutor]{basic_type_arrays_tutor} -->
|
||||
|
||||
@@ -184,7 +184,7 @@ map[10583] = "小鸭";
|
||||
|
||||
/* 查询操作 */
|
||||
// 向哈希表中输入键 key ,得到值 value
|
||||
String name = map[15937];
|
||||
String? name = map[15937];
|
||||
|
||||
/* 删除操作 */
|
||||
// 在哈希表中删除键值对 (key, value)
|
||||
@@ -202,7 +202,7 @@ let mut map: HashMap<i32, String> = HashMap::new();
|
||||
map.insert(12836, "小哈".to_string());
|
||||
map.insert(15937, "小啰".to_string());
|
||||
map.insert(16750, "小算".to_string());
|
||||
map.insert(13279, "小法".to_string());
|
||||
map.insert(13276, "小法".to_string());
|
||||
map.insert(10583, "小鸭".to_string());
|
||||
|
||||
/* 查询操作 */
|
||||
|
||||
@@ -71,7 +71,7 @@ bool isEmpty = maxHeap.empty();
|
||||
|
||||
/* 输入列表并建堆 */
|
||||
vector<int> input{1, 3, 2, 5, 4};
|
||||
priority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());
|
||||
minHeap = priority_queue<int, vector<int>, greater<int>>(input.begin(), input.end());
|
||||
```
|
||||
|
||||
```java title="heap.java"
|
||||
|
||||
@@ -28,8 +28,8 @@ class ListNode {
|
||||
```csharp title=""
|
||||
/* Linked list node class */
|
||||
class ListNode(int x) { // Constructor
|
||||
int val = x; // Node value
|
||||
ListNode? next; // Reference to the next node
|
||||
public int val = x; // Node value
|
||||
public ListNode? next; // Reference to the next node
|
||||
}
|
||||
```
|
||||
|
||||
@@ -264,7 +264,7 @@ n3.next = n4;
|
||||
```
|
||||
|
||||
```dart title="linked_list.dart"
|
||||
/* Initialize the linked list 1 -> 3 -> 2 -> 5 -> 4 */\
|
||||
/* Initialize the linked list 1 -> 3 -> 2 -> 5 -> 4 */
|
||||
// Initialize the nodes
|
||||
ListNode n0 = ListNode(1);
|
||||
ListNode n1 = ListNode(3);
|
||||
@@ -375,9 +375,9 @@ class ListNode {
|
||||
```csharp title=""
|
||||
/* Doubly linked list node class */
|
||||
class ListNode(int x) { // Constructor
|
||||
int val = x; // Node value
|
||||
ListNode next; // Reference to the successor
|
||||
ListNode prev; // Reference to the predecessor
|
||||
public int val = x; // Node value
|
||||
public ListNode? next; // Reference to the successor
|
||||
public ListNode? prev; // Reference to the predecessor
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ List<int> nums = [1, 3, 2, 5, 4];
|
||||
// Without initial values
|
||||
let nums1: Vec<i32> = Vec::new();
|
||||
// With initial values
|
||||
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
let mut nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
```
|
||||
|
||||
```c title="list.c"
|
||||
@@ -404,10 +404,10 @@ nums.add(5);
|
||||
nums.add(4);
|
||||
|
||||
/* Insert an element in the middle */
|
||||
nums.add(3, 6); // Insert 6 at index 3
|
||||
nums.add(3, 6); // Insert 6 at index 3
|
||||
|
||||
/* Remove an element */
|
||||
nums.remove(3); // Remove the element at index 3
|
||||
nums.removeAt(3); // Remove the element at index 3
|
||||
```
|
||||
|
||||
```ruby title="list.rb"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<!-- [snippet]{backtrack_framework} -->
|
||||
```python title=""
|
||||
def backtrack(state: State, choices: list[choice], res: list[state]):
|
||||
def backtrack(state: State, choices: list[Choice], res: list[State]):
|
||||
"""Backtracking template"""
|
||||
# Check whether the state is a solution
|
||||
if is_solution(state):
|
||||
@@ -30,7 +30,7 @@ void backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {
|
||||
return;
|
||||
}
|
||||
// Try every choice
|
||||
for (Choice choice : choices) {
|
||||
for (Choice *choice : choices) {
|
||||
// Prune: check whether the choice is valid
|
||||
if (isValid(state, choice)) {
|
||||
// Try: make the choice and update the state
|
||||
@@ -189,7 +189,7 @@ function backtrack(state: State, choices: Choice[], res: State[]): void {
|
||||
|
||||
```dart title=""
|
||||
/* Backtracking template */
|
||||
void backtrack(State state, List<Choice>, List<State> res) {
|
||||
void backtrack(State state, List<Choice> choices, List<State> res) {
|
||||
// Check whether the state is a solution
|
||||
if (isSolution(state)) {
|
||||
// Record the solution
|
||||
|
||||
@@ -196,7 +196,7 @@ function algorithm(n: number): number { // Input data
|
||||
/* Class */
|
||||
class Node {
|
||||
int val;
|
||||
Node next;
|
||||
Node? next;
|
||||
Node(this.val, [this.next]);
|
||||
}
|
||||
|
||||
@@ -332,8 +332,9 @@ void algorithm(int n) {
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
int[] b = new int[10000]; // O(1)
|
||||
if (n > 10)
|
||||
if (n > 10) {
|
||||
int[] nums = new int[n]; // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -353,7 +354,7 @@ func algorithm(n int) {
|
||||
b := make([]int, 10000) // O(1)
|
||||
var nums []int
|
||||
if n > 10 {
|
||||
nums := make([]int, n) // O(n)
|
||||
nums = make([]int, n) // O(n)
|
||||
}
|
||||
fmt.Println(a, b, nums)
|
||||
}
|
||||
@@ -413,8 +414,9 @@ fn algorithm(n: i32) {
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
int b[10000]; // O(1)
|
||||
if (n > 10)
|
||||
int nums[n] = {0}; // O(n)
|
||||
if (n > 10) {
|
||||
int nums[n]; // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -613,7 +615,7 @@ fn function() -> i32 {
|
||||
return 0;
|
||||
}
|
||||
/* The loop uses O(1) space */
|
||||
fn loop(n: i32) {
|
||||
fn for_loop(n: i32) {
|
||||
for i in 0..n {
|
||||
function();
|
||||
}
|
||||
|
||||
@@ -774,10 +774,10 @@ def algorithm(n)
|
||||
a = 1 # +0 (shortcut 1)
|
||||
a = a + n # +0 (shortcut 1)
|
||||
# +n (shortcut 2)
|
||||
(0...(5 * n + 1)).each do { puts 0 }
|
||||
(0...(5 * n + 1)).each { puts 0 }
|
||||
# +n*n (shortcut 3)
|
||||
(0...(2 * n)).each do
|
||||
(0...(n + 1)).each do { puts 0 }
|
||||
(0...(n + 1)).each { puts 0 }
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
@@ -96,7 +96,7 @@ val bools = BooleanArray(5)
|
||||
|
||||
```ruby title=""
|
||||
# Ruby arrays can hold different basic data types and object references
|
||||
data = [0, 0.0, 'a', false, ListNode(0)]
|
||||
data = [0, 0.0, 'a', false, ListNode.new(0)]
|
||||
```
|
||||
|
||||
<!-- [tutor]{basic_type_arrays_tutor} -->
|
||||
|
||||
@@ -184,7 +184,7 @@ map[10583] = "小鸭";
|
||||
|
||||
/* Lookup */
|
||||
// Look up key in the hash table to obtain value
|
||||
String name = map[15937];
|
||||
String? name = map[15937];
|
||||
|
||||
/* Deletion */
|
||||
// Remove a key-value pair (key, value) from the hash table
|
||||
@@ -202,7 +202,7 @@ let mut map: HashMap<i32, String> = HashMap::new();
|
||||
map.insert(12836, "小哈".to_string());
|
||||
map.insert(15937, "小啰".to_string());
|
||||
map.insert(16750, "小算".to_string());
|
||||
map.insert(13279, "小法".to_string());
|
||||
map.insert(13276, "小法".to_string());
|
||||
map.insert(10583, "小鸭".to_string());
|
||||
|
||||
/* Lookup */
|
||||
|
||||
@@ -71,7 +71,7 @@ bool isEmpty = maxHeap.empty();
|
||||
|
||||
/* Build a heap from a list */
|
||||
vector<int> input{1, 3, 2, 5, 4};
|
||||
priority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());
|
||||
minHeap = priority_queue<int, vector<int>, greater<int>>(input.begin(), input.end());
|
||||
```
|
||||
|
||||
```java title="heap.java"
|
||||
|
||||
@@ -28,8 +28,8 @@ class ListNode {
|
||||
```csharp title=""
|
||||
/* 連結リストのノードクラス */
|
||||
class ListNode(int x) { //コンストラクタ
|
||||
int val = x; // ノードの値
|
||||
ListNode? next; // 次のノードへの参照
|
||||
public int val = x; // ノードの値
|
||||
public ListNode? next; // 次のノードへの参照
|
||||
}
|
||||
```
|
||||
|
||||
@@ -264,7 +264,7 @@ n3.next = n4;
|
||||
```
|
||||
|
||||
```dart title="linked_list.dart"
|
||||
/* 連結リスト 1 -> 3 -> 2 -> 5 -> 4 を初期化 */\
|
||||
/* 連結リスト 1 -> 3 -> 2 -> 5 -> 4 を初期化 */
|
||||
// 各ノードを初期化
|
||||
ListNode n0 = ListNode(1);
|
||||
ListNode n1 = ListNode(3);
|
||||
@@ -375,9 +375,9 @@ class ListNode {
|
||||
```csharp title=""
|
||||
/* 双方向連結リストのノードクラス */
|
||||
class ListNode(int x) { // コンストラクタ
|
||||
int val = x; // ノードの値
|
||||
ListNode next; // 後続ノードへの参照
|
||||
ListNode prev; // 先行ノードへの参照
|
||||
public int val = x; // ノードの値
|
||||
public ListNode? next; // 後続ノードへの参照
|
||||
public ListNode? prev; // 先行ノードへの参照
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ List<int> nums = [1, 3, 2, 5, 4];
|
||||
// 初期値なし
|
||||
let nums1: Vec<i32> = Vec::new();
|
||||
// 初期値あり
|
||||
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
let mut nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
```
|
||||
|
||||
```c title="list.c"
|
||||
@@ -404,10 +404,10 @@ nums.add(5);
|
||||
nums.add(4);
|
||||
|
||||
/* 途中に要素を挿入 */
|
||||
nums.add(3, 6); // インデックス 3 に数値 6 を挿入
|
||||
nums.add(3, 6); // インデックス 3 に数値 6 を挿入
|
||||
|
||||
/* 要素を削除 */
|
||||
nums.remove(3); // インデックス 3 の要素を削除
|
||||
nums.removeAt(3); // インデックス 3 の要素を削除
|
||||
```
|
||||
|
||||
```ruby title="list.rb"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<!-- [snippet]{backtrack_framework} -->
|
||||
```python title=""
|
||||
def backtrack(state: State, choices: list[choice], res: list[state]):
|
||||
def backtrack(state: State, choices: list[Choice], res: list[State]):
|
||||
"""バックトラッキングの基本形"""
|
||||
# 解かどうかを判定
|
||||
if is_solution(state):
|
||||
@@ -30,7 +30,7 @@ void backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を調べる
|
||||
for (Choice choice : choices) {
|
||||
for (Choice *choice : choices) {
|
||||
// 枝刈り:選択が有効かを判定
|
||||
if (isValid(state, choice)) {
|
||||
// 試行:選択して状態を更新
|
||||
@@ -189,7 +189,7 @@ function backtrack(state: State, choices: Choice[], res: State[]): void {
|
||||
|
||||
```dart title=""
|
||||
/* バックトラッキングの基本形 */
|
||||
void backtrack(State state, List<Choice>, List<State> res) {
|
||||
void backtrack(State state, List<Choice> choices, List<State> res) {
|
||||
// 解かどうかを判定
|
||||
if (isSolution(state)) {
|
||||
// 解を記録
|
||||
|
||||
@@ -196,7 +196,7 @@ function algorithm(n: number): number { // 入力データ
|
||||
/* クラス */
|
||||
class Node {
|
||||
int val;
|
||||
Node next;
|
||||
Node? next;
|
||||
Node(this.val, [this.next]);
|
||||
}
|
||||
|
||||
@@ -332,8 +332,9 @@ void algorithm(int n) {
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
int[] b = new int[10000]; // O(1)
|
||||
if (n > 10)
|
||||
if (n > 10) {
|
||||
int[] nums = new int[n]; // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -353,7 +354,7 @@ func algorithm(n int) {
|
||||
b := make([]int, 10000) // O(1)
|
||||
var nums []int
|
||||
if n > 10 {
|
||||
nums := make([]int, n) // O(n)
|
||||
nums = make([]int, n) // O(n)
|
||||
}
|
||||
fmt.Println(a, b, nums)
|
||||
}
|
||||
@@ -413,8 +414,9 @@ fn algorithm(n: i32) {
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
int b[10000]; // O(1)
|
||||
if (n > 10)
|
||||
int nums[n] = {0}; // O(n)
|
||||
if (n > 10) {
|
||||
int nums[n]; // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -613,7 +615,7 @@ fn function() -> i32 {
|
||||
return 0;
|
||||
}
|
||||
/* ループの空間計算量は O(1) */
|
||||
fn loop(n: i32) {
|
||||
fn for_loop(n: i32) {
|
||||
for i in 0..n {
|
||||
function();
|
||||
}
|
||||
|
||||
@@ -774,10 +774,10 @@ def algorithm(n)
|
||||
a = 1 # +0(工夫 1)
|
||||
a = a + n # +0(工夫 1)
|
||||
# +n(工夫 2)
|
||||
(0...(5 * n + 1)).each do { puts 0 }
|
||||
(0...(5 * n + 1)).each { puts 0 }
|
||||
# +n*n(工夫 3)
|
||||
(0...(2 * n)).each do
|
||||
(0...(n + 1)).each do { puts 0 }
|
||||
(0...(n + 1)).each { puts 0 }
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
@@ -96,7 +96,7 @@ val bools = BooleanArray(5)
|
||||
|
||||
```ruby title=""
|
||||
# Ruby のリストには、さまざまな基本データ型やオブジェクトへの参照を自由に格納できる
|
||||
data = [0, 0.0, 'a', false, ListNode(0)]
|
||||
data = [0, 0.0, 'a', false, ListNode.new(0)]
|
||||
```
|
||||
|
||||
<!-- [tutor]{basic_type_arrays_tutor} -->
|
||||
|
||||
@@ -184,7 +184,7 @@ map[10583] = "小鸭";
|
||||
|
||||
/* 照会操作 */
|
||||
// キー key を入力して値 value を取得
|
||||
String name = map[15937];
|
||||
String? name = map[15937];
|
||||
|
||||
/* 削除操作 */
|
||||
// ハッシュテーブルからペア (key, value) を削除
|
||||
@@ -202,7 +202,7 @@ let mut map: HashMap<i32, String> = HashMap::new();
|
||||
map.insert(12836, "小哈".to_string());
|
||||
map.insert(15937, "小啰".to_string());
|
||||
map.insert(16750, "小算".to_string());
|
||||
map.insert(13279, "小法".to_string());
|
||||
map.insert(13276, "小法".to_string());
|
||||
map.insert(10583, "小鸭".to_string());
|
||||
|
||||
/* 照会操作 */
|
||||
|
||||
@@ -71,7 +71,7 @@ bool isEmpty = maxHeap.empty();
|
||||
|
||||
/* リストを入力してヒープを構築 */
|
||||
vector<int> input{1, 3, 2, 5, 4};
|
||||
priority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());
|
||||
minHeap = priority_queue<int, vector<int>, greater<int>>(input.begin(), input.end());
|
||||
```
|
||||
|
||||
```java title="heap.java"
|
||||
|
||||
@@ -28,8 +28,8 @@ class ListNode {
|
||||
```csharp title=""
|
||||
/* 연결 리스트 노드 클래스 */
|
||||
class ListNode(int x) { //생성자
|
||||
int val = x; // 노드 값
|
||||
ListNode? next; // 다음 노드를 가리키는 참조
|
||||
public int val = x; // 노드 값
|
||||
public ListNode? next; // 다음 노드를 가리키는 참조
|
||||
}
|
||||
```
|
||||
|
||||
@@ -264,7 +264,7 @@ n3.next = n4;
|
||||
```
|
||||
|
||||
```dart title="linked_list.dart"
|
||||
/* 연결 리스트 1 -> 3 -> 2 -> 5 -> 4 초기화 */\
|
||||
/* 연결 리스트 1 -> 3 -> 2 -> 5 -> 4 초기화 */
|
||||
// 각 노드 초기화
|
||||
ListNode n0 = ListNode(1);
|
||||
ListNode n1 = ListNode(3);
|
||||
@@ -375,9 +375,9 @@ class ListNode {
|
||||
```csharp title=""
|
||||
/* 이중 연결 리스트 노드 클래스 */
|
||||
class ListNode(int x) { // 생성자
|
||||
int val = x; // 노드 값
|
||||
ListNode next; // 후속 노드를 가리키는 참조
|
||||
ListNode prev; // 선행 노드를 가리키는 참조
|
||||
public int val = x; // 노드 값
|
||||
public ListNode? next; // 후속 노드를 가리키는 참조
|
||||
public ListNode? prev; // 선행 노드를 가리키는 참조
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ List<int> nums = [1, 3, 2, 5, 4];
|
||||
// 초깃값 없음
|
||||
let nums1: Vec<i32> = Vec::new();
|
||||
// 초깃값 지정
|
||||
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
let mut nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
```
|
||||
|
||||
```c title="list.c"
|
||||
@@ -404,10 +404,10 @@ nums.add(5);
|
||||
nums.add(4);
|
||||
|
||||
/* 중간에 원소 삽입 */
|
||||
nums.add(3, 6); // 인덱스 3에 숫자 6 삽입
|
||||
nums.add(3, 6); // 인덱스 3에 숫자 6 삽입
|
||||
|
||||
/* 원소 삭제 */
|
||||
nums.remove(3); // 인덱스 3의 원소 삭제
|
||||
nums.removeAt(3); // 인덱스 3의 원소 삭제
|
||||
```
|
||||
|
||||
```ruby title="list.rb"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<!-- [snippet]{backtrack_framework} -->
|
||||
```python title=""
|
||||
def backtrack(state: State, choices: list[choice], res: list[state]):
|
||||
def backtrack(state: State, choices: list[Choice], res: list[State]):
|
||||
"""백트래킹 알고리즘의 기본 틀"""
|
||||
# 해인지 확인
|
||||
if is_solution(state):
|
||||
@@ -30,7 +30,7 @@ void backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {
|
||||
return;
|
||||
}
|
||||
// 모든 선택지 순회
|
||||
for (Choice choice : choices) {
|
||||
for (Choice *choice : choices) {
|
||||
// 가지치기: 선택이 유효한지 확인
|
||||
if (isValid(state, choice)) {
|
||||
// 시도: 선택하고 상태 갱신
|
||||
@@ -189,7 +189,7 @@ function backtrack(state: State, choices: Choice[], res: State[]): void {
|
||||
|
||||
```dart title=""
|
||||
/* 백트래킹 알고리즘의 기본 틀 */
|
||||
void backtrack(State state, List<Choice>, List<State> res) {
|
||||
void backtrack(State state, List<Choice> choices, List<State> res) {
|
||||
// 해인지 확인
|
||||
if (isSolution(state)) {
|
||||
// 해 기록
|
||||
|
||||
@@ -196,7 +196,7 @@ function algorithm(n: number): number { // 입력 데이터
|
||||
/* 클래스 */
|
||||
class Node {
|
||||
int val;
|
||||
Node next;
|
||||
Node? next;
|
||||
Node(this.val, [this.next]);
|
||||
}
|
||||
|
||||
@@ -332,8 +332,9 @@ void algorithm(int n) {
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
int[] b = new int[10000]; // O(1)
|
||||
if (n > 10)
|
||||
if (n > 10) {
|
||||
int[] nums = new int[n]; // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -353,7 +354,7 @@ func algorithm(n int) {
|
||||
b := make([]int, 10000) // O(1)
|
||||
var nums []int
|
||||
if n > 10 {
|
||||
nums := make([]int, n) // O(n)
|
||||
nums = make([]int, n) // O(n)
|
||||
}
|
||||
fmt.Println(a, b, nums)
|
||||
}
|
||||
@@ -413,8 +414,9 @@ fn algorithm(n: i32) {
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
int b[10000]; // O(1)
|
||||
if (n > 10)
|
||||
int nums[n] = {0}; // O(n)
|
||||
if (n > 10) {
|
||||
int nums[n]; // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -613,7 +615,7 @@ fn function() -> i32 {
|
||||
return 0;
|
||||
}
|
||||
/* 반복문의 공간 복잡도는 O(1) */
|
||||
fn loop(n: i32) {
|
||||
fn for_loop(n: i32) {
|
||||
for i in 0..n {
|
||||
function();
|
||||
}
|
||||
|
||||
@@ -774,10 +774,10 @@ def algorithm(n)
|
||||
a = 1 # +0(요령 1)
|
||||
a = a + n # +0(요령 1)
|
||||
# +n(요령 2)
|
||||
(0...(5 * n + 1)).each do { puts 0 }
|
||||
(0...(5 * n + 1)).each { puts 0 }
|
||||
# +n*n(요령 3)
|
||||
(0...(2 * n)).each do
|
||||
(0...(n + 1)).each do { puts 0 }
|
||||
(0...(n + 1)).each { puts 0 }
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
@@ -96,7 +96,7 @@ val bools = BooleanArray(5)
|
||||
|
||||
```ruby title=""
|
||||
# Ruby의 리스트는 여러 기본 자료형과 객체 참조를 자유롭게 저장할 수 있다
|
||||
data = [0, 0.0, 'a', false, ListNode(0)]
|
||||
data = [0, 0.0, 'a', false, ListNode.new(0)]
|
||||
```
|
||||
|
||||
<!-- [tutor]{basic_type_arrays_tutor} -->
|
||||
|
||||
@@ -184,7 +184,7 @@ map[10583] = "小鸭";
|
||||
|
||||
/* 조회 연산 */
|
||||
// 해시 테이블에 키 key를 입력하여 값 value를 얻음
|
||||
String name = map[15937];
|
||||
String? name = map[15937];
|
||||
|
||||
/* 삭제 연산 */
|
||||
// 해시 테이블에서 키-값 쌍 (key, value) 삭제
|
||||
@@ -202,7 +202,7 @@ let mut map: HashMap<i32, String> = HashMap::new();
|
||||
map.insert(12836, "小哈".to_string());
|
||||
map.insert(15937, "小啰".to_string());
|
||||
map.insert(16750, "小算".to_string());
|
||||
map.insert(13279, "小法".to_string());
|
||||
map.insert(13276, "小法".to_string());
|
||||
map.insert(10583, "小鸭".to_string());
|
||||
|
||||
/* 조회 연산 */
|
||||
|
||||
@@ -71,7 +71,7 @@ bool isEmpty = maxHeap.empty();
|
||||
|
||||
/* 리스트를 입력받아 힙 구성 */
|
||||
vector<int> input{1, 3, 2, 5, 4};
|
||||
priority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());
|
||||
minHeap = priority_queue<int, vector<int>, greater<int>>(input.begin(), input.end());
|
||||
```
|
||||
|
||||
```java title="heap.java"
|
||||
|
||||
@@ -28,8 +28,8 @@ class ListNode {
|
||||
```csharp title=""
|
||||
/* Класс узла связного списка */
|
||||
class ListNode(int x) { // Конструктор
|
||||
int val = x; // Значение узла
|
||||
ListNode? next; // Ссылка на следующий узел
|
||||
public int val = x; // Значение узла
|
||||
public ListNode? next; // Ссылка на следующий узел
|
||||
}
|
||||
```
|
||||
|
||||
@@ -264,7 +264,7 @@ n3.next = n4;
|
||||
```
|
||||
|
||||
```dart title="linked_list.dart"
|
||||
/* Инициализация связного списка 1 -> 3 -> 2 -> 5 -> 4 */\
|
||||
/* Инициализация связного списка 1 -> 3 -> 2 -> 5 -> 4 */
|
||||
// Инициализация каждого узла
|
||||
ListNode n0 = ListNode(1);
|
||||
ListNode n1 = ListNode(3);
|
||||
@@ -375,9 +375,9 @@ class ListNode {
|
||||
```csharp title=""
|
||||
/* Класс узла двусвязного списка */
|
||||
class ListNode(int x) { // Конструктор
|
||||
int val = x; // Значение узла
|
||||
ListNode next; // Ссылка на следующий узел
|
||||
ListNode prev; // Ссылка на предыдущий узел
|
||||
public int val = x; // Значение узла
|
||||
public ListNode? next; // Ссылка на следующий узел
|
||||
public ListNode? prev; // Ссылка на предыдущий узел
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ List<int> nums = [1, 3, 2, 5, 4];
|
||||
// Без начальных значений
|
||||
let nums1: Vec<i32> = Vec::new();
|
||||
// С начальными значениями
|
||||
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
let mut nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
```
|
||||
|
||||
```c title="list.c"
|
||||
@@ -404,10 +404,10 @@ nums.add(5);
|
||||
nums.add(4);
|
||||
|
||||
/* Вставить элемент в середину */
|
||||
nums.add(3, 6); // Вставить число 6 по индексу 3
|
||||
nums.add(3, 6); // Вставить число 6 по индексу 3
|
||||
|
||||
/* Удалить элемент */
|
||||
nums.remove(3); // Удалить элемент по индексу 3
|
||||
nums.removeAt(3); // Удалить элемент по индексу 3
|
||||
```
|
||||
|
||||
```ruby title="list.rb"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<!-- [snippet]{backtrack_framework} -->
|
||||
```python title=""
|
||||
def backtrack(state: State, choices: list[choice], res: list[state]):
|
||||
def backtrack(state: State, choices: list[Choice], res: list[State]):
|
||||
"""Каркас алгоритма поиска с возвратом"""
|
||||
# Проверка, является ли текущее состояние решением
|
||||
if is_solution(state):
|
||||
@@ -30,7 +30,7 @@ void backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {
|
||||
return;
|
||||
}
|
||||
// Перебор всех возможных выборов
|
||||
for (Choice choice : choices) {
|
||||
for (Choice *choice : choices) {
|
||||
// Обрезка: проверка допустимости выбора
|
||||
if (isValid(state, choice)) {
|
||||
// Попытка: сделать выбор и обновить состояние
|
||||
@@ -189,7 +189,7 @@ function backtrack(state: State, choices: Choice[], res: State[]): void {
|
||||
|
||||
```dart title=""
|
||||
/* Каркас алгоритма поиска с возвратом */
|
||||
void backtrack(State state, List<Choice>, List<State> res) {
|
||||
void backtrack(State state, List<Choice> choices, List<State> res) {
|
||||
// Проверка, является ли текущее состояние решением
|
||||
if (isSolution(state)) {
|
||||
// Запись решения
|
||||
|
||||
@@ -196,7 +196,7 @@ function algorithm(n: number): number { // Входные данные
|
||||
/* Класс */
|
||||
class Node {
|
||||
int val;
|
||||
Node next;
|
||||
Node? next;
|
||||
Node(this.val, [this.next]);
|
||||
}
|
||||
|
||||
@@ -332,8 +332,9 @@ void algorithm(int n) {
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
int[] b = new int[10000]; // O(1)
|
||||
if (n > 10)
|
||||
if (n > 10) {
|
||||
int[] nums = new int[n]; // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -353,7 +354,7 @@ func algorithm(n int) {
|
||||
b := make([]int, 10000) // O(1)
|
||||
var nums []int
|
||||
if n > 10 {
|
||||
nums := make([]int, n) // O(n)
|
||||
nums = make([]int, n) // O(n)
|
||||
}
|
||||
fmt.Println(a, b, nums)
|
||||
}
|
||||
@@ -413,8 +414,9 @@ fn algorithm(n: i32) {
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
int b[10000]; // O(1)
|
||||
if (n > 10)
|
||||
int nums[n] = {0}; // O(n)
|
||||
if (n > 10) {
|
||||
int nums[n]; // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -613,7 +615,7 @@ fn function() -> i32 {
|
||||
return 0;
|
||||
}
|
||||
/* Пространственная сложность цикла равна O(1) */
|
||||
fn loop(n: i32) {
|
||||
fn for_loop(n: i32) {
|
||||
for i in 0..n {
|
||||
function();
|
||||
}
|
||||
|
||||
@@ -774,10 +774,10 @@ def algorithm(n)
|
||||
a = 1 # +0 (прием 1)
|
||||
a = a + n # +0 (прием 1)
|
||||
# +n (прием 2)
|
||||
(0...(5 * n + 1)).each do { puts 0 }
|
||||
(0...(5 * n + 1)).each { puts 0 }
|
||||
# +n*n (прием 3)
|
||||
(0...(2 * n)).each do
|
||||
(0...(n + 1)).each do { puts 0 }
|
||||
(0...(n + 1)).each { puts 0 }
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
@@ -96,7 +96,7 @@ val bools = BooleanArray(5)
|
||||
|
||||
```ruby title=""
|
||||
# Списки Ruby могут свободно хранить различные базовые типы данных и ссылки на объекты
|
||||
data = [0, 0.0, 'a', false, ListNode(0)]
|
||||
data = [0, 0.0, 'a', false, ListNode.new(0)]
|
||||
```
|
||||
|
||||
<!-- [tutor]{basic_type_arrays_tutor} -->
|
||||
|
||||
@@ -184,7 +184,7 @@ map[10583] = "Сяо Я";
|
||||
|
||||
/* Операция поиска */
|
||||
// Передать в хеш-таблицу ключ key и получить значение value
|
||||
String name = map[15937];
|
||||
String? name = map[15937];
|
||||
|
||||
/* Операция удаления */
|
||||
// Удалить пару ключ-значение (key, value) из хеш-таблицы
|
||||
@@ -202,7 +202,7 @@ let mut map: HashMap<i32, String> = HashMap::new();
|
||||
map.insert(12836, "Сяо Ха".to_string());
|
||||
map.insert(15937, "Сяо Ло".to_string());
|
||||
map.insert(16750, "Сяо Суань".to_string());
|
||||
map.insert(13279, "Сяо Фа".to_string());
|
||||
map.insert(13276, "Сяо Фа".to_string());
|
||||
map.insert(10583, "Сяо Я".to_string());
|
||||
|
||||
/* Операция поиска */
|
||||
|
||||
@@ -71,7 +71,7 @@ bool isEmpty = maxHeap.empty();
|
||||
|
||||
/* Построение кучи из входного списка */
|
||||
vector<int> input{1, 3, 2, 5, 4};
|
||||
priority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());
|
||||
minHeap = priority_queue<int, vector<int>, greater<int>>(input.begin(), input.end());
|
||||
```
|
||||
|
||||
```java title="heap.java"
|
||||
|
||||
@@ -28,8 +28,8 @@ class ListNode {
|
||||
```csharp title=""
|
||||
/* 鏈結串列節點類別 */
|
||||
class ListNode(int x) { //建構子
|
||||
int val = x; // 節點值
|
||||
ListNode? next; // 指向下一節點的引用
|
||||
public int val = x; // 節點值
|
||||
public ListNode? next; // 指向下一節點的引用
|
||||
}
|
||||
```
|
||||
|
||||
@@ -264,7 +264,7 @@ n3.next = n4;
|
||||
```
|
||||
|
||||
```dart title="linked_list.dart"
|
||||
/* 初始化鏈結串列 1 -> 3 -> 2 -> 5 -> 4 */\
|
||||
/* 初始化鏈結串列 1 -> 3 -> 2 -> 5 -> 4 */
|
||||
// 初始化各個節點
|
||||
ListNode n0 = ListNode(1);
|
||||
ListNode n1 = ListNode(3);
|
||||
@@ -375,9 +375,9 @@ class ListNode {
|
||||
```csharp title=""
|
||||
/* 雙向鏈結串列節點類別 */
|
||||
class ListNode(int x) { // 建構子
|
||||
int val = x; // 節點值
|
||||
ListNode next; // 指向後繼節點的引用
|
||||
ListNode prev; // 指向前驅節點的引用
|
||||
public int val = x; // 節點值
|
||||
public ListNode? next; // 指向後繼節點的引用
|
||||
public ListNode? prev; // 指向前驅節點的引用
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ List<int> nums = [1, 3, 2, 5, 4];
|
||||
// 無初始值
|
||||
let nums1: Vec<i32> = Vec::new();
|
||||
// 有初始值
|
||||
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
let mut nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
```
|
||||
|
||||
```c title="list.c"
|
||||
@@ -404,10 +404,10 @@ nums.add(5);
|
||||
nums.add(4);
|
||||
|
||||
/* 在中間插入元素 */
|
||||
nums.add(3, 6); // 在索引 3 處插入數字 6
|
||||
nums.add(3, 6); // 在索引 3 處插入數字 6
|
||||
|
||||
/* 刪除元素 */
|
||||
nums.remove(3); // 刪除索引 3 處的元素
|
||||
nums.removeAt(3); // 刪除索引 3 處的元素
|
||||
```
|
||||
|
||||
```ruby title="list.rb"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<!-- [snippet]{backtrack_framework} -->
|
||||
```python title=""
|
||||
def backtrack(state: State, choices: list[choice], res: list[state]):
|
||||
def backtrack(state: State, choices: list[Choice], res: list[State]):
|
||||
"""回溯演算法框架"""
|
||||
# 判斷是否為解
|
||||
if is_solution(state):
|
||||
@@ -30,7 +30,7 @@ void backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {
|
||||
return;
|
||||
}
|
||||
// 走訪所有選擇
|
||||
for (Choice choice : choices) {
|
||||
for (Choice *choice : choices) {
|
||||
// 剪枝:判斷選擇是否合法
|
||||
if (isValid(state, choice)) {
|
||||
// 嘗試:做出選擇,更新狀態
|
||||
@@ -189,7 +189,7 @@ function backtrack(state: State, choices: Choice[], res: State[]): void {
|
||||
|
||||
```dart title=""
|
||||
/* 回溯演算法框架 */
|
||||
void backtrack(State state, List<Choice>, List<State> res) {
|
||||
void backtrack(State state, List<Choice> choices, List<State> res) {
|
||||
// 判斷是否為解
|
||||
if (isSolution(state)) {
|
||||
// 記錄解
|
||||
|
||||
@@ -196,7 +196,7 @@ function algorithm(n: number): number { // 輸入資料
|
||||
/* 類別 */
|
||||
class Node {
|
||||
int val;
|
||||
Node next;
|
||||
Node? next;
|
||||
Node(this.val, [this.next]);
|
||||
}
|
||||
|
||||
@@ -332,8 +332,9 @@ void algorithm(int n) {
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
int[] b = new int[10000]; // O(1)
|
||||
if (n > 10)
|
||||
if (n > 10) {
|
||||
int[] nums = new int[n]; // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -353,7 +354,7 @@ func algorithm(n int) {
|
||||
b := make([]int, 10000) // O(1)
|
||||
var nums []int
|
||||
if n > 10 {
|
||||
nums := make([]int, n) // O(n)
|
||||
nums = make([]int, n) // O(n)
|
||||
}
|
||||
fmt.Println(a, b, nums)
|
||||
}
|
||||
@@ -413,8 +414,9 @@ fn algorithm(n: i32) {
|
||||
void algorithm(int n) {
|
||||
int a = 0; // O(1)
|
||||
int b[10000]; // O(1)
|
||||
if (n > 10)
|
||||
int nums[n] = {0}; // O(n)
|
||||
if (n > 10) {
|
||||
int nums[n]; // O(n)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -613,7 +615,7 @@ fn function() -> i32 {
|
||||
return 0;
|
||||
}
|
||||
/* 迴圈的空間複雜度為 O(1) */
|
||||
fn loop(n: i32) {
|
||||
fn for_loop(n: i32) {
|
||||
for i in 0..n {
|
||||
function();
|
||||
}
|
||||
|
||||
@@ -774,10 +774,10 @@ def algorithm(n)
|
||||
a = 1 # +0(技巧 1)
|
||||
a = a + n # +0(技巧 1)
|
||||
# +n(技巧 2)
|
||||
(0...(5 * n + 1)).each do { puts 0 }
|
||||
(0...(5 * n + 1)).each { puts 0 }
|
||||
# +n*n(技巧 3)
|
||||
(0...(2 * n)).each do
|
||||
(0...(n + 1)).each do { puts 0 }
|
||||
(0...(n + 1)).each { puts 0 }
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
@@ -96,7 +96,7 @@ val bools = BooleanArray(5)
|
||||
|
||||
```ruby title=""
|
||||
# Ruby 的串列可以自由儲存各種基本資料型別和物件引用
|
||||
data = [0, 0.0, 'a', false, ListNode(0)]
|
||||
data = [0, 0.0, 'a', false, ListNode.new(0)]
|
||||
```
|
||||
|
||||
<!-- [tutor]{basic_type_arrays_tutor} -->
|
||||
|
||||
@@ -184,7 +184,7 @@ map[10583] = "小鴨";
|
||||
|
||||
/* 查詢操作 */
|
||||
// 向雜湊表中輸入鍵 key ,得到值 value
|
||||
String name = map[15937];
|
||||
String? name = map[15937];
|
||||
|
||||
/* 刪除操作 */
|
||||
// 在雜湊表中刪除鍵值對 (key, value)
|
||||
@@ -202,7 +202,7 @@ let mut map: HashMap<i32, String> = HashMap::new();
|
||||
map.insert(12836, "小哈".to_string());
|
||||
map.insert(15937, "小囉".to_string());
|
||||
map.insert(16750, "小算".to_string());
|
||||
map.insert(13279, "小法".to_string());
|
||||
map.insert(13276, "小法".to_string());
|
||||
map.insert(10583, "小鴨".to_string());
|
||||
|
||||
/* 查詢操作 */
|
||||
|
||||
@@ -71,7 +71,7 @@ bool isEmpty = maxHeap.empty();
|
||||
|
||||
/* 輸入串列並建堆積 */
|
||||
vector<int> input{1, 3, 2, 5, 4};
|
||||
priority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());
|
||||
minHeap = priority_queue<int, vector<int>, greater<int>>(input.begin(), input.end());
|
||||
```
|
||||
|
||||
```java title="heap.java"
|
||||
|
||||
Reference in New Issue
Block a user