Add Dart codes to the documents. (#529)

This commit is contained in:
Yudong Jin
2023-06-02 02:40:26 +08:00
committed by GitHub
parent 041a989d33
commit 025051c81b
38 changed files with 849 additions and 96 deletions

View File

@@ -6,70 +6,68 @@
import 'dart:math';
class Array {
/* 随机返回一个 数组元素 */
int randomAccess(List nums) {
// 在区间[0,size) 中随机抽取一个数字
int randomIndex = Random().nextInt(nums.length);
// 获取并返回随机元素
int randomNum = nums[randomIndex];
return randomNum;
}
/* 随机返回一个 数组元素 */
int randomAccess(List nums) {
// 在区间[0,size) 中随机抽取一个数字
int randomIndex = Random().nextInt(nums.length);
// 获取并返回随机元素
int randomNum = nums[randomIndex];
return randomNum;
}
/* 扩展数组长度 */
List extend(List nums, int enlarge) {
// 初始化一个扩展长度后的数组元素初始值为0
List<int> res = List.filled(nums.length + enlarge, 0);
/* 扩展数组长度 */
List extend(List nums, int enlarge) {
// 初始化一个扩展长度后的数组元素初始值为0
List<int> res = List.filled(nums.length + enlarge, 0);
// 将原数组中的所有元素复制到新数组
for (var i = 0; i < nums.length; i++) {
res[i] = nums[i];
}
// 返回扩展后的新数组
return res;
// 将原数组中的所有元素复制到新数组
for (var i = 0; i < nums.length; i++) {
res[i] = nums[i];
}
// 返回扩展后的新数组
return res;
}
/* 在数组的索引 index 处插入元素 num */
void insert(List nums, int num, int index) {
// 把索引index以及之后的所有元素向后移动一位
for (var i = nums.length - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// 将 num 赋给 index 处元素
nums[index] = num;
/* 在数组的索引 index 处插入元素 num */
void insert(List nums, int num, int index) {
// 把索引index以及之后的所有元素向后移动一位
for (var i = nums.length - 1; i > index; i--) {
nums[i] = nums[i - 1];
}
// 将 num 赋给 index 处元素
nums[index] = num;
}
/* 删除索引 index 处元素 */
void remove(List nums, int index) {
for (var i = index; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
/* 删除索引 index 处元素 */
void remove(List nums, int index) {
for (var i = index; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
}
/* 遍历数组元素 */
void traverse(List nums) {
var count = 0;
// 通过索引遍历数组
for (var i = 0; i < nums.length; i++) {
count++;
}
// 直接遍历数组
for (var num in nums) {
count++;
}
// 通过forEach方法遍历数组
nums.forEach((element) {
count++;
});
/* 遍历数组元素 */
void traverse(List nums) {
var count = 0;
// 通过索引遍历数组
for (var i = 0; i < nums.length; i++) {
count++;
}
// 直接遍历数组
for (var num in nums) {
count++;
}
// 通过forEach方法遍历数组
nums.forEach((element) {
count++;
});
}
/* 在数组中查找指定元素 */
int find(List nums, int target) {
for (var i = 0; i < nums.length; i++) {
if (nums[i] == target) return i;
}
return -1;
/* 在数组中查找指定元素 */
int find(List nums, int target) {
for (var i = 0; i < nums.length; i++) {
if (nums[i] == target) return i;
}
return -1;
}
/* Driver Code */
@@ -81,26 +79,26 @@ int main() {
print('数组 nums = $nums');
/* 随机访问 */
int randomNum = Array().randomAccess(nums);
int randomNum = randomAccess(nums);
print('在 nums 中获取随机元素 $randomNum');
/* 长度扩展 */
nums = Array().extend(nums, 3);
nums = extend(nums, 3);
print('将数组长度扩展至 8 ,得到 nums = $nums');
/* 插入元素 */
Array().insert(nums, 6, 3);
insert(nums, 6, 3);
print("在索引 3 处插入数字 6 ,得到 nums = $nums");
/* 删除元素 */
Array().remove(nums, 2);
remove(nums, 2);
print("删除索引 2 处的元素,得到 nums = $nums");
/* 遍历元素 */
Array().traverse(nums);
traverse(nums);
/* 查找元素 */
int index = Array().find(nums, 3);
int index = find(nums, 3);
print("在 nums 中查找元素 3 ,得到索引 = $index");
return 0;

View File

@@ -7,43 +7,41 @@
import '../utils/list_node.dart';
import '../utils/print_util.dart';
class LinkedList {
/* 在链表的节点 n0 之后插入节点 P */
void insert(ListNode n0, ListNode P) {
ListNode? n1 = n0.next;
P.next = n1;
n0.next = P;
}
/* 在链表的节点 n0 之后插入节点 P */
void insert(ListNode n0, ListNode P) {
ListNode? n1 = n0.next;
P.next = n1;
n0.next = P;
}
/* 删除链表的节点 n0 之后的首个节点 */
void remove(ListNode n0) {
if (n0.next == null) return;
ListNode P = n0.next!;
ListNode? n1 = P.next;
n0.next = n1;
}
/* 删除链表的节点 n0 之后的首个节点 */
void remove(ListNode n0) {
if (n0.next == null) return;
ListNode P = n0.next!;
ListNode? n1 = P.next;
n0.next = n1;
}
/* 访问链表中索引为 index 的节点 */
ListNode? access(ListNode? head, int index) {
for (var i = 0; i < index; i++) {
if (head == null) return null;
head = head.next;
/* 访问链表中索引为 index 的节点 */
ListNode? access(ListNode? head, int index) {
for (var i = 0; i < index; i++) {
if (head == null) return null;
head = head.next;
}
return head;
}
/* 在链表中查找值为 target 的首个节点 */
int find(ListNode? head, int target) {
int index = 0;
while (head != null) {
if (head.val == target) {
return index;
}
return head;
}
/* 在链表中查找值为 target 的首个节点 */
int find(ListNode? head, int target) {
int index = 0;
while (head != null) {
if (head.val == target) {
return index;
}
head = head.next;
index++;
}
return -1;
head = head.next;
index++;
}
return -1;
}
/* Driver Code */
@@ -65,21 +63,21 @@ int main() {
printLinkedList(n0);
/* 插入节点 */
LinkedList().insert(n0, ListNode(0));
insert(n0, ListNode(0));
print('插入节点后的链表为');
printLinkedList(n0);
/* 删除节点 */
LinkedList().remove(n0);
remove(n0);
print('删除节点后的链表为');
printLinkedList(n0);
/* 访问节点 */
ListNode? node = LinkedList().access(n0, 3);
ListNode? node = access(n0, 3);
print('链表中索引 3 处的节点的值 = ${node!.val}');
/* 查找节点 */
int index = LinkedList().find(n0, 2);
int index = find(n0, 2);
print('链表中值为 2 的节点的索引 = $index');
return 0;

View File

@@ -24,6 +24,7 @@ int findOne(List<int> nums) {
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
if (nums[i] == 1) return i;
}
return -1;
}
@@ -36,5 +37,6 @@ int main() {
print('\n数组 [ 1, 2, ..., n ] 被打乱后 = $nums');
print('数字 1 的索引为 + $index');
}
return 0;
}