2023-01-06 03:39:19 +08:00
|
|
|
/**
|
2022-12-13 23:24:12 +08:00
|
|
|
* File: space_complexity.java
|
2022-11-25 02:04:38 +08:00
|
|
|
* Created Time: 2022-11-25
|
|
|
|
* Author: Krahets (krahets@163.com)
|
|
|
|
*/
|
|
|
|
|
2022-11-12 22:48:46 +08:00
|
|
|
package chapter_computational_complexity;
|
2022-11-08 02:58:42 +08:00
|
|
|
|
|
|
|
import include.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
2022-12-13 23:24:12 +08:00
|
|
|
public class space_complexity {
|
2022-11-08 02:58:42 +08:00
|
|
|
/* 函数 */
|
|
|
|
static int function() {
|
|
|
|
// do something
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 常数阶 */
|
|
|
|
static void constant(int n) {
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
final int a = 0;
|
|
|
|
int b = 0;
|
|
|
|
int[] nums = new int[10000];
|
|
|
|
ListNode node = new ListNode(0);
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
int c = 0;
|
|
|
|
}
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
function();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 线性阶 */
|
|
|
|
static void linear(int n) {
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
int[] nums = new int[n];
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
|
|
|
List<ListNode> nodes = new ArrayList<>();
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
nodes.add(new ListNode(i));
|
|
|
|
}
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
|
|
|
Map<Integer, String> map = new HashMap<>();
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
map.put(i, String.valueOf(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 线性阶(递归实现) */
|
|
|
|
static void linearRecur(int n) {
|
|
|
|
System.out.println("递归 n = " + n);
|
|
|
|
if (n == 1) return;
|
|
|
|
linearRecur(n - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 平方阶 */
|
|
|
|
static void quadratic(int n) {
|
|
|
|
// 矩阵占用 O(n^2) 空间
|
2022-11-27 04:20:30 +08:00
|
|
|
int[][] numMatrix = new int[n][n];
|
2022-11-08 02:58:42 +08:00
|
|
|
// 二维列表占用 O(n^2) 空间
|
|
|
|
List<List<Integer>> numList = new ArrayList<>();
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
List<Integer> tmp = new ArrayList<>();
|
|
|
|
for (int j = 0; j < n; j++) {
|
|
|
|
tmp.add(0);
|
|
|
|
}
|
|
|
|
numList.add(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 平方阶(递归实现) */
|
|
|
|
static int quadraticRecur(int n) {
|
|
|
|
if (n <= 0) return 0;
|
|
|
|
int[] nums = new int[n];
|
|
|
|
System.out.println("递归 n = " + n + " 中的 nums 长度 = " + nums.length);
|
|
|
|
return quadraticRecur(n - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 指数阶(建立满二叉树) */
|
|
|
|
static TreeNode buildTree(int n) {
|
|
|
|
if (n == 0) return null;
|
|
|
|
TreeNode root = new TreeNode(0);
|
|
|
|
root.left = buildTree(n - 1);
|
|
|
|
root.right = buildTree(n - 1);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
public static void main(String[] args) {
|
|
|
|
int n = 5;
|
|
|
|
// 常数阶
|
|
|
|
constant(n);
|
|
|
|
// 线性阶
|
|
|
|
linear(n);
|
|
|
|
linearRecur(n);
|
|
|
|
// 平方阶
|
|
|
|
quadratic(n);
|
|
|
|
quadraticRecur(n);
|
|
|
|
// 指数阶
|
2022-11-25 20:12:20 +08:00
|
|
|
TreeNode root = buildTree(n);
|
|
|
|
PrintUtil.printTree(root);
|
2022-11-08 02:58:42 +08:00
|
|
|
}
|
|
|
|
}
|