Add dart chapter_computational_complexity (#363)

* add dart chapter_array_and_linkedlist

* update my_list.dart

* update chapter_array_and_linkedlist

* Update my_list.dart

* Update array.dart

* Update file name

* Add chapter_computational_complexity

* Add chapter_computational_complexity

* add space_complexity class and format code

* remove class

---------

Co-authored-by: huangjianqing <huangjianqing@52tt.com>
Co-authored-by: Yudong Jin <krahets@163.com>
pull/371/head
Jefferson 2 years ago committed by GitHub
parent 6812b4f5c5
commit 7b9c552273
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,8 +4,9 @@
* Author: Jefferson (JeffersonHuang77@gmail.com) * Author: Jefferson (JeffersonHuang77@gmail.com)
*/ */
import '../utils/ListNode.dart'; import '../utils/list_node.dart';
import '../utils/PrintUtil.dart'; import '../utils/print_util.dart';
class LinkedList { class LinkedList {
/* 在链表的结点 n0 之后插入结点 P */ /* 在链表的结点 n0 之后插入结点 P */
@ -62,15 +63,15 @@ int main() {
n3.next = n4; n3.next = n4;
print('初始化的链表为'); print('初始化的链表为');
PrintUtil().printLinkedList(n0); printLinkedList(n0);
/* 插入结点 */ /* 插入结点 */
LinkedList().insert(n0, ListNode(0)); LinkedList().insert(n0, ListNode(0));
PrintUtil().printLinkedList(n0); printLinkedList(n0);
/* 删除结点 */ /* 删除结点 */
LinkedList().remove(n0); LinkedList().remove(n0);
PrintUtil().printLinkedList(n0); printLinkedList(n0);
/* 访问结点 */ /* 访问结点 */
ListNode? node = LinkedList().access(n0, 3); ListNode? node = LinkedList().access(n0, 3);

@ -0,0 +1,47 @@
/**
* File: leetcode_two_sum.dart
* Created Time: 2023-2-11
* Author: Jefferson (JeffersonHuang77@gmail.com)
*/
import 'dart:collection';
/* 方法一: 暴力枚举 */
List<int> twoSumBruteForce(List<int> nums, int target) {
int size = nums.length;
for (var i = 0; i < size - 1; i++) {
for (var j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target) return [i, j];
}
}
return [0];
}
/* 方法二: 辅助哈希表 */
List<int> twoSumHashTable(List<int> nums, int target) {
int size = nums.length;
Map<int, int> dic = HashMap();
for (var i = 0; i < size; i++) {
if (dic.containsKey(target - nums[i])) {
return [dic[target - nums[i]]!, i];
}
dic.putIfAbsent(nums[i], () => i);
}
return [0];
}
/* Driver Code */
int main() {
// ======= Test Case =======
List<int> nums = [2, 7, 11, 15];
int target = 9;
// ====== Driver Code ======
//
List<int> res = twoSumBruteForce(nums, target);
print('方法一 res = $res');
//
res = twoSumHashTable(nums, target);
print('方法二 res = $res');
return 0;
}

@ -0,0 +1,106 @@
/**
* File: space_complexity.dart
* Created Time: 2023-2-12
* Author: Jefferson (JeffersonHuang77@gmail.com)
*/
import 'dart:collection';
import '../utils/list_node.dart';
import '../utils/print_util.dart';
import '../utils/tree_node.dart';
/* 函数 */
int function() {
// do something
return 0;
}
/* 常数阶 */
void constant(int n) {
// O(1)
final int a = 0;
int b = 0;
List<int> nums = List.filled(10000, 0);
// O(1)
for (var i = 0; i < n; i++) {
int c = 0;
}
// O(1)
for (var i = 0; i < n; i++) {
function();
}
}
/* 线性阶 */
void linear(int n) {
// n O(n)
List<int> nums = List.filled(n, 0);
// n O(n)
List<ListNode> nodes = [];
for (var i = 0; i < n; i++) {
nodes.add(ListNode(i));
}
// n O(n)
Map<int, String> map = HashMap();
for (var i = 0; i < n; i++) {
map.putIfAbsent(i, () => i.toString());
}
}
/* 线性阶(递归实现) */
void linearRecur(int n) {
print('递归 n = $n');
if (n == 1) return;
linearRecur(n - 1);
}
/* 平方阶 */
void quadratic(int n) {
// O(n^2)
List<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));
// O(n^2)
List<List<int>> numList = [];
for (var i = 0; i < n; i++) {
List<int> tmp = [];
for (int j = 0; j < n; j++) {
tmp.add(0);
}
numList.add(tmp);
}
}
/* 平方阶(递归实现) */
int quadraticRecur(int n) {
if (n <= 0) return 0;
List<int> nums = List.filled(n, 0);
print('递归 n = $n 中的长度 nums 长度 = ${nums.length}');
return quadraticRecur(n - 1);
}
/* 指数阶(建立满二叉树) */
TreeNode? buildTree(int n) {
if (n == 0) return null;
TreeNode root = TreeNode(n);
root.left = buildTree(n - 1);
root.right = buildTree(n - 1);
return root;
}
/* Driver Code */
int main() {
int n = 5;
//
constant(n);
// 线
linear(n);
linearRecur(n);
//
quadratic(n);
quadraticRecur(n);
//
TreeNode? root = buildTree(n);
printTree(root);
return 0;
}

@ -0,0 +1,163 @@
/**
* File: time_complexity
* Created Time: 2023-02-12
* Author: Jefferson (JeffersonHuang77@gmail.com)
*/
/* 常数阶 */
int constant(int n) {
int count = 0;
int size = 100000;
for (var i = 0; i < size; i++) {
count++;
}
return count;
}
/* 线性阶 */
int linear(int n) {
int count = 0;
for (var i = 0; i < n; i++) {
count++;
}
return count;
}
/* 线性阶(遍历数组) */
int arrayTraversal(List<int> nums) {
int count = 0;
//
for (var num in nums) {
count++;
}
return count;
}
/* 平方阶 */
int quadratic(int n) {
int count = 0;
//
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count++;
}
}
return count;
}
/* 平方阶(冒泡排序) */
int bubbleSort(List<int> nums) {
int count = 0; //
// n-1, n-2, ..., 1
for (var i = nums.length - 1; i > 0; i--) {
//
for (var j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// nums[j] nums[j + 1]
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
count += 3; // 3
}
}
}
return count;
}
/* 指数阶(循环实现) */
int exponential(int n) {
int count = 0, base = 1;
// cell 1, 2, 4, 8, ..., 2^(n-1)
for (var i = 0; i < n; i++) {
for (var j = 0; j < base; j++) {
count++;
}
base *= 2;
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count;
}
/* 指数阶(递归实现) */
int expRecur(int n) {
if (n == 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
/* 对数阶(循环实现) */
int logarithmic(num n) {
int count = 0;
while (n > 1) {
n = n / 2;
count++;
}
return count;
}
/* 对数阶(递归实现) */
int logRecur(num n) {
if (n <= 1) return 0;
return logRecur(n / 2) + 1;
}
/* 线性对数阶 */
int linearLogRecur(num n) {
if (n <= 1) return 1;
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
for (var i = 0; i < n; i++) {
count++;
}
return count;
}
/* 阶乘阶(递归实现) */
int factorialRecur(int n) {
if (n == 0) return 1;
int count = 0;
// 1 n
for (var i = 0; i < n; i++) {
count += factorialRecur(n - 1);
}
return count;
}
int main() {
// n
int n = 8;
print('输入数据大小 n = $n');
int count = constant(n);
print('常数阶的计算操作数量 = $count');
count = linear(n);
print('线性阶的计算操作数量 = $count');
count = arrayTraversal(List.filled(n, 0));
print('线性阶(遍历数组)的计算操作数量 = $count');
count = quadratic(n);
print('平方阶的计算操作数量 = $count');
final nums = List.filled(n, 0);
for (int i = 0; i < n; i++) {
nums[i] = n - i; // [n,n-1,...,2,1]
}
count = bubbleSort(nums);
print('平方阶(冒泡排序)的计算操作数量 = $count');
count = exponential(n);
print('指数阶(循环实现)的计算操作数量 = $count');
count = expRecur(n);
print('指数阶(递归实现)的计算操作数量 = $count');
count = logarithmic(n);
print('对数阶(循环实现)的计算操作数量 = $count');
count = logRecur(n);
print('对数阶(递归实现)的计算操作数量 = $count');
count = linearLogRecur(n);
print('线性对数阶(递归实现)的计算操作数量 = $count');
count = factorialRecur(n);
print('阶乘阶(递归实现)的计算操作数量 = $count');
return 0;
}

@ -0,0 +1,39 @@
/**
* File: worst_best_time_complexity
* Created Time: 2023-02-12
* Author: Jefferson (JeffersonHuang77@gmail.com)
*/
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */
List<int> randomNumbers(int n) {
final nums = List.filled(n, 0);
// nums = { 1, 2, 3, ..., n }
for (var i = 0; i < n; i++) {
nums[i] = i + 1;
}
//
nums.shuffle();
return nums;
}
/* 查找数组 nums 中数字 1 所在索引 */
int findOne(List<int> nums) {
for (var i = 0; i < nums.length; i++) {
// 1 O(1)
// 1 O(n)
if (nums[i] == 1) return i;
}
return -1;
}
int main() {
for (var i = 0; i < 10; i++) {
int n = 100;
final nums = randomNumbers(n);
int index = findOne(nums);
print('\n数组 [ 1, 2, ..., n ] 被打乱后 = $nums');
print('数字 1 的索引为 + $index');
}
return 0;
}

@ -1,21 +0,0 @@
/**
* File: PrintUtil
* Created Time: 2023-01-23
* Author: Jefferson (JeffersonHuang77@gmail.com)
*/
import 'ListNode.dart';
class PrintUtil {
void printLinkedList(ListNode? head) {
List<String> list = [];
while (head != null) {
list.add('${head.val}');
head = head.next;
}
print(list.join(' -> '));
}
}

@ -0,0 +1,72 @@
/**
* File: PrintUtil
* Created Time: 2023-01-23
* Author: Jefferson (JeffersonHuang77@gmail.com)
*/
import 'dart:io';
import 'list_node.dart';
import 'tree_node.dart';
class Trunk {
Trunk? prev;
String str;
Trunk(this.prev, this.str);
}
void printLinkedList(ListNode? head) {
List<String> list = [];
while (head != null) {
list.add('${head.val}');
head = head.next;
}
print(list.join(' -> '));
}
/*
* Print a binary tree
* @param root
* @param prev
* @param isLeft
*/
void printTree(TreeNode? root, [Trunk? prev = null, bool isLeft = false]) {
if (root == null) {
return;
}
String prev_str = ' ';
Trunk trunk = Trunk(prev, prev_str);
printTree(root.right, trunk, true);
if (prev == null) {
trunk.str = '---';
} else if (isLeft) {
trunk.str = '/---';
prev_str = ' |';
} else {
trunk.str = '\\---';
prev.str = prev_str;
}
showTrunks(trunk);
print(' ${root.val}');
if (prev != null) {
prev.str = prev_str;
}
trunk.str = ' |';
printTree(root.left, trunk, false);
}
void showTrunks(Trunk? p) {
if (p == null) {
return;
}
showTrunks(p.prev);
stdout.write(p.str);
}

@ -0,0 +1,69 @@
/**
* File: tree_node.dart
* Created Time: 2023-2-12
* Author: Jefferson (JeffersonHuang77@gmail.com)
*/
import 'dart:collection';
class TreeNode {
late int val; //
late int height; //
late TreeNode? left; //
late TreeNode? right; //
TreeNode(int x) {
val = x;
}
}
/**
* Generate a binary tree given an array
* @param list
* @return
*/
TreeNode? listToTree(List<int> list) {
int size = list.length;
if (size == 0) return null;
TreeNode root = TreeNode(list[0]);
Queue<TreeNode?> queue = Queue();
queue.add(root);
int i = 0;
while (!queue.isEmpty) {
TreeNode? node = queue.first;
queue.removeFirst();
if (++i >= size) break;
node?.left = TreeNode(list[i]);
queue.add(node?.left);
if (++i >= size) break;
node?.left = TreeNode(list[i]);
queue.add(node?.right);
}
return root;
}
/**
* Serialize a binary tree to a list
* @param root
* @return
*/
List<int?> treeToList(TreeNode? root) {
List<int?> list = [];
if (root == null) return list;
Queue<TreeNode?> queue = Queue();
queue.add(root);
while (!queue.isEmpty) {
TreeNode? node = queue.first;
queue.removeFirst();
if (node != null) {
list.add(node.val);
queue.add(node.left);
queue.add(node.right);
} else {
list.add(null);
}
}
return list;
}
Loading…
Cancel
Save