mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	* .net 8.0 migration * update docs * revert change * revert change and update appendix docs * remove static * Update binary_search_insertion.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs --------- Co-authored-by: Yudong Jin <krahets@163.com>
		
			
				
	
	
		
			105 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			105 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
/**
 | 
						|
 * File: space_complexity.cs
 | 
						|
 * Created Time: 2022-12-23
 | 
						|
 * Author: haptear (haptear@hotmail.com)
 | 
						|
 */
 | 
						|
 | 
						|
namespace hello_algo.chapter_computational_complexity;
 | 
						|
 | 
						|
public class space_complexity {
 | 
						|
    /* 函数 */
 | 
						|
    int Function() {
 | 
						|
        // 执行某些操作
 | 
						|
        return 0;
 | 
						|
    }
 | 
						|
 | 
						|
    /* 常数阶 */
 | 
						|
    void Constant(int n) {
 | 
						|
        // 常量、变量、对象占用 O(1) 空间
 | 
						|
        int a = 0;
 | 
						|
        int b = 0;
 | 
						|
        int[] nums = new int[10000];
 | 
						|
        ListNode node = new(0);
 | 
						|
        // 循环中的变量占用 O(1) 空间
 | 
						|
        for (int i = 0; i < n; i++) {
 | 
						|
            int c = 0;
 | 
						|
        }
 | 
						|
        // 循环中的函数占用 O(1) 空间
 | 
						|
        for (int i = 0; i < n; i++) {
 | 
						|
            Function();
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /* 线性阶 */
 | 
						|
    void Linear(int n) {
 | 
						|
        // 长度为 n 的数组占用 O(n) 空间
 | 
						|
        int[] nums = new int[n];
 | 
						|
        // 长度为 n 的列表占用 O(n) 空间
 | 
						|
        List<ListNode> nodes = [];
 | 
						|
        for (int i = 0; i < n; i++) {
 | 
						|
            nodes.Add(new ListNode(i));
 | 
						|
        }
 | 
						|
        // 长度为 n 的哈希表占用 O(n) 空间
 | 
						|
        Dictionary<int, string> map = [];
 | 
						|
        for (int i = 0; i < n; i++) {
 | 
						|
            map.Add(i, i.ToString());
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /* 线性阶(递归实现) */
 | 
						|
    void LinearRecur(int n) {
 | 
						|
        Console.WriteLine("递归 n = " + n);
 | 
						|
        if (n == 1) return;
 | 
						|
        LinearRecur(n - 1);
 | 
						|
    }
 | 
						|
 | 
						|
    /* 平方阶 */
 | 
						|
    void Quadratic(int n) {
 | 
						|
        // 矩阵占用 O(n^2) 空间
 | 
						|
        int[,] numMatrix = new int[n, n];
 | 
						|
        // 二维列表占用 O(n^2) 空间
 | 
						|
        List<List<int>> numList = [];
 | 
						|
        for (int 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;
 | 
						|
        int[] nums = new int[n];
 | 
						|
        Console.WriteLine("递归 n = " + n + " 中的 nums 长度 = " + nums.Length);
 | 
						|
        return QuadraticRecur(n - 1);
 | 
						|
    }
 | 
						|
 | 
						|
    /* 指数阶(建立满二叉树) */
 | 
						|
    TreeNode? BuildTree(int n) {
 | 
						|
        if (n == 0) return null;
 | 
						|
        TreeNode root = new(0) {
 | 
						|
            left = BuildTree(n - 1),
 | 
						|
            right = BuildTree(n - 1)
 | 
						|
        };
 | 
						|
        return root;
 | 
						|
    }
 | 
						|
 | 
						|
    [Test]
 | 
						|
    public void Test() {
 | 
						|
        int n = 5;
 | 
						|
        // 常数阶
 | 
						|
        Constant(n);
 | 
						|
        // 线性阶
 | 
						|
        Linear(n);
 | 
						|
        LinearRecur(n);
 | 
						|
        // 平方阶
 | 
						|
        Quadratic(n);
 | 
						|
        QuadraticRecur(n);
 | 
						|
        // 指数阶
 | 
						|
        TreeNode? root = BuildTree(n);
 | 
						|
        PrintUtil.PrintTree(root);
 | 
						|
    }
 | 
						|
}
 |