mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
feat: Revised the book (#978)
* Sync recent changes to the revised Word. * Revised the preface chapter * Revised the introduction chapter * Revised the computation complexity chapter * Revised the chapter data structure * Revised the chapter array and linked list * Revised the chapter stack and queue * Revised the chapter hashing * Revised the chapter tree * Revised the chapter heap * Revised the chapter graph * Revised the chapter searching * Reivised the sorting chapter * Revised the divide and conquer chapter * Revised the chapter backtacking * Revised the DP chapter * Revised the greedy chapter * Revised the appendix chapter * Revised the preface chapter doubly * Revised the figures
This commit is contained in:
@@ -39,7 +39,7 @@ void insert(List<int> nums, int _num, int index) {
|
||||
nums[index] = _num;
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
/* 删除索引 index 处的元素 */
|
||||
void remove(List<int> nums, int index) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (var i = index; i < nums.length - 1; i++) {
|
||||
|
||||
@@ -54,7 +54,7 @@ void main() {
|
||||
ListNode n2 = ListNode(2);
|
||||
ListNode n3 = ListNode(5);
|
||||
ListNode n4 = ListNode(4);
|
||||
// 构建引用指向
|
||||
// 构建节点之间的引用
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
|
||||
@@ -24,7 +24,7 @@ void main() {
|
||||
nums.clear();
|
||||
print('清空列表后 nums = $nums');
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
@@ -32,7 +32,7 @@ void main() {
|
||||
nums.add(4);
|
||||
print('添加元素后 nums = $nums');
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums.insert(3, 6);
|
||||
print('在索引 3 处插入数字 6 ,得到 nums = $nums');
|
||||
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
/* 列表类简易实现 */
|
||||
/* 列表类 */
|
||||
class MyList {
|
||||
late List<int> _arr; // 数组(存储列表元素)
|
||||
int _capacity = 10; // 列表容量
|
||||
int _size = 0; // 列表长度(即当前元素数量)
|
||||
int _size = 0; // 列表长度(当前元素数量)
|
||||
int _extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造方法 */
|
||||
@@ -16,7 +16,7 @@ class MyList {
|
||||
_arr = List.filled(_capacity, 0);
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
/* 获取列表长度(当前元素数量)*/
|
||||
int size() => _size;
|
||||
|
||||
/* 获取列表容量 */
|
||||
@@ -34,7 +34,7 @@ class MyList {
|
||||
_arr[index] = _num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
void add(int _num) {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (_size == _capacity) extendCapacity();
|
||||
@@ -43,7 +43,7 @@ class MyList {
|
||||
_size++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
void insert(int index, int _num) {
|
||||
if (index >= _size) throw RangeError('索引越界');
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
@@ -97,7 +97,7 @@ class MyList {
|
||||
void main() {
|
||||
/* 初始化列表 */
|
||||
MyList nums = MyList();
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
@@ -106,7 +106,7 @@ void main() {
|
||||
print(
|
||||
'列表 nums = ${nums.toArray()} ,容量 = ${nums.capacity()} ,长度 = ${nums.size()}');
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums.insert(3, 6);
|
||||
print('在索引 3 处插入数字 6 ,得到 nums = ${nums.toArray()}');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user