mirror of
https://github.com/krahets/hello-algo.git
synced 2025-01-23 14:20:29 +08:00
Update Dart code to output same as Java (#522)
This commit is contained in:
parent
5bbcb12979
commit
8247a611d7
@ -86,7 +86,7 @@ int main() {
|
||||
|
||||
/* 长度扩展 */
|
||||
nums = Array().extend(nums, 3);
|
||||
print('将数组长度扩展至 8, 得到nums = $nums');
|
||||
print('将数组长度扩展至 8 ,得到 nums = $nums');
|
||||
|
||||
/* 插入元素 */
|
||||
Array().insert(nums, 6, 3);
|
||||
|
@ -7,7 +7,6 @@
|
||||
import '../utils/list_node.dart';
|
||||
import '../utils/print_util.dart';
|
||||
|
||||
|
||||
class LinkedList {
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
void insert(ListNode n0, ListNode P) {
|
||||
@ -67,10 +66,12 @@ int main() {
|
||||
|
||||
/* 插入节点 */
|
||||
LinkedList().insert(n0, ListNode(0));
|
||||
print('插入节点后的链表为');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 删除节点 */
|
||||
LinkedList().remove(n0);
|
||||
print('删除节点后的链表为');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 访问节点 */
|
||||
|
@ -7,7 +7,7 @@
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化列表 */
|
||||
List<int> list = [1,3,2,5,4];
|
||||
List<int> list = [1, 3, 2, 5, 4];
|
||||
print('列表 list = $list');
|
||||
|
||||
/* 访问元素 */
|
||||
@ -35,7 +35,8 @@ int main() {
|
||||
print('在索引 3 处插入数字 6 ,得到 list = $list');
|
||||
|
||||
/* 删除元素 */
|
||||
list.remove(3);
|
||||
list.removeAt(3);
|
||||
print('删除索引 3 处的元素,得到 list = $list');
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
@ -49,7 +50,7 @@ int main() {
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
List<int> list1 = [6,8,7,10,9];
|
||||
List<int> list1 = [6, 8, 7, 10, 9];
|
||||
list.addAll(list1);
|
||||
print('将列表 list1 拼接到 list 之后,得到 list = $list');
|
||||
|
||||
|
@ -103,7 +103,8 @@ int main() {
|
||||
list.add(2);
|
||||
list.add(5);
|
||||
list.add(4);
|
||||
print('列表 list = ${list.toArray()},容量 = ${list.capacity()},长度 = ${list.size()}');
|
||||
print(
|
||||
'列表 list = ${list.toArray()} ,容量 = ${list.capacity()} ,长度 = ${list.size()}');
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.insert(3, 6);
|
||||
@ -126,7 +127,8 @@ int main() {
|
||||
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
|
||||
list.add(i);
|
||||
}
|
||||
print('扩容后的列表 list = ${list.toArray()},容量 = ${list.capacity()} ,长度 = ${list.size()}');
|
||||
print(
|
||||
'扩容后的列表 list = ${list.toArray()} ,容量 = ${list.capacity()} ,长度 = ${list.size()}');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -75,14 +75,14 @@ void quadratic(int n) {
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
List<int> nums = List.filled(n, 0);
|
||||
print('递归 n = $n 中的长度 nums 长度 = ${nums.length}');
|
||||
print('递归 n = $n 中的 nums 长度 = ${nums.length}');
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
TreeNode? buildTree(int n) {
|
||||
if (n == 0) return null;
|
||||
TreeNode root = TreeNode(n);
|
||||
TreeNode root = TreeNode(0);
|
||||
root.left = buildTree(n - 1);
|
||||
root.right = buildTree(n - 1);
|
||||
return root;
|
||||
|
@ -85,8 +85,7 @@ class GraphAdjMat {
|
||||
|
||||
/* 打印邻接矩阵 */
|
||||
void printAdjMat() {
|
||||
print("顶点列表 = ");
|
||||
print(vertices);
|
||||
print("顶点列表 = $vertices");
|
||||
print("邻接矩阵 = ");
|
||||
printMatrix(adjMat);
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ class MaxHeap {
|
||||
/* 交换元素 */
|
||||
void _swap(int i, int j) {
|
||||
int tmp = _maxHeap[i];
|
||||
_maxHeap[i] = _maxHeap[j];;
|
||||
_maxHeap[i] = _maxHeap[j];
|
||||
_maxHeap[j] = tmp;
|
||||
}
|
||||
|
||||
@ -127,7 +127,7 @@ void main() {
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
int peek = maxHeap.peek();
|
||||
print("\n堆顶元素为:$peek");
|
||||
print("\n堆顶元素为 $peek");
|
||||
|
||||
/* 元素入堆 */
|
||||
int val = 7;
|
||||
|
@ -33,7 +33,7 @@ void bucketSort(List<double> nums) {
|
||||
/* Driver Code*/
|
||||
void main() {
|
||||
// 设输入数据为浮点数,范围为 [0, 1)
|
||||
final nums = [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68];
|
||||
final nums = [0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37];
|
||||
bucketSort(nums);
|
||||
print('桶排序完成后 = $nums');
|
||||
print('桶排序完成后 nums = $nums');
|
||||
}
|
||||
|
@ -138,7 +138,7 @@ void main() {
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
final int size = deque.size();
|
||||
print("双向队列的长度 size = $size");
|
||||
print("双向队列长度 size = $size");
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
final bool isEmpty = deque.isEmpty();
|
||||
|
@ -99,7 +99,7 @@ void main() {
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
final bool isEmpty = queue.isEmpty();
|
||||
print("队列是否为空 isEmpty = $isEmpty");
|
||||
print("队列是否为空 = $isEmpty");
|
||||
|
||||
/* 测试环形数组 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
|
@ -9,13 +9,9 @@ import 'dart:collection';
|
||||
void main() {
|
||||
/* 初始化双向队列 */
|
||||
final Queue<int> deque = Queue();
|
||||
|
||||
/* 元素入队 */
|
||||
deque.addFirst(3);
|
||||
deque.addLast(2);
|
||||
deque.addLast(5);
|
||||
deque.addLast(4);
|
||||
deque.addFirst(3);
|
||||
deque.addFirst(1);
|
||||
print("双向队列 deque = $deque");
|
||||
|
||||
/* 访问元素 */
|
||||
@ -24,17 +20,24 @@ void main() {
|
||||
final int peekLast = deque.last;
|
||||
print("队尾元素 peekLast = $peekLast");
|
||||
|
||||
/* 元素入队 */
|
||||
deque.addLast(4);
|
||||
print("元素 4 队尾入队后 deque = $deque");
|
||||
deque.addFirst(1);
|
||||
print("元素 1 队首入队后 deque = $deque");
|
||||
|
||||
/* 元素出队 */
|
||||
final int popFirst = deque.removeFirst();
|
||||
print("队首出队元素 popFirst = $popFirst,队首出队后 deque = $deque");
|
||||
|
||||
final int popLast = deque.removeLast();
|
||||
print("队尾出队元素 popLast = $popLast,队尾出队后 deque = $deque");
|
||||
print("队尾出队元素 = $popLast,队尾出队后 deque = $deque");
|
||||
final int popFirst = deque.removeFirst();
|
||||
print("队首出队元素 = $popFirst,队首出队后 deque = $deque");
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
final int size = deque.length;
|
||||
print("双向队列的长度 size = $size");
|
||||
print("双向队列长度 size = $size");
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
final bool isEmpty = deque.isEmpty;
|
||||
print("双向队列是否为空 isEmpty = $isEmpty");
|
||||
print("双向队列是否为空 = $isEmpty");
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ void main() {
|
||||
|
||||
/* 元素出队 */
|
||||
final int pop = queue.removeFirst();
|
||||
print("出队元素 pop = $pop,出队后队列 queue = $queue");
|
||||
print("出队元素 pop = $pop,出队后 queue = $queue");
|
||||
|
||||
/* 获取队列长度 */
|
||||
final int size = queue.length;
|
||||
@ -33,5 +33,5 @@ void main() {
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
final bool isEmpty = queue.isEmpty;
|
||||
print("队列是否为空 isEmpty = $isEmpty");
|
||||
print("队列是否为空 = $isEmpty");
|
||||
}
|
||||
|
@ -23,8 +23,7 @@ void main() {
|
||||
|
||||
/* 元素出栈 */
|
||||
final int pop = stack.removeLast();
|
||||
print("出栈元素 pop = $pop");
|
||||
print("出栈后 stack = $stack");
|
||||
print("出栈元素 pop = $pop,出栈后 stack = $stack");
|
||||
|
||||
/* 获取栈的长度 */
|
||||
final int size = stack.length;
|
||||
|
@ -46,12 +46,12 @@ void printTree(TreeNode? root, [Trunk? prev = null, bool isLeft = false]) {
|
||||
printTree(root.right, trunk, true);
|
||||
|
||||
if (prev == null) {
|
||||
trunk.str = '---';
|
||||
trunk.str = '———';
|
||||
} else if (isLeft) {
|
||||
trunk.str = '/---';
|
||||
trunk.str = '/———';
|
||||
prev_str = ' |';
|
||||
} else {
|
||||
trunk.str = '\\---';
|
||||
trunk.str = '\\———';
|
||||
prev.str = prev_str;
|
||||
}
|
||||
showTrunks(trunk);
|
||||
|
Loading…
Reference in New Issue
Block a user