• 时间复杂度、空间复杂度 O(1)和 O(logN)


    时间复杂度(time complexity):估算程序指令的执行次数(执行时间)
    空间复杂度(space complexity):估算所需占用的存储空间
    在这里插入图片描述

    public static void test1(int n) {
    		
    		// 确定的执行次数
    		if (n > 10) { 
    			System.out.println("n > 10");
    		} else if (n > 5) { // 2
    			System.out.println("n > 5");
    		} else {
    			System.out.println("n <= 5"); 
    		}
    		
    		// 1 + 4 + 4 + 4
    		for (int i = 0; i < 4; i++) {
    			System.out.println("test");
    		}
    		
    		// 都是O(1)
    	}
    
    	public static void test2(int n) {
    		// O(n)
    		for (int i = 0; i < n; i++) {
    			System.out.println("test");
    		}
    	}
    
    	public static void test3(int n) {
    		// 1 + 2n + n * (1 + 3n)
    		// 1 + 2n + n + 3n^2
    		// 3n^2 + 3n + 1
    		// O(n^2)
    		
    		// O(n^2)
    		for (int i = 0; i < n; i++) {
    			for (int j = 0; j < n; j++) {
    				System.out.println("test");
    			}
    		}
    	}
    
    	public static void test4(int n) {
    		// 1 + 2n + n * (1 + 45)
    		// 1 + 2n + 46n
    		// 48n + 1
    		// O(n)
    		for (int i = 0; i < n; i++) {
    			for (int j = 0; j < 15; j++) {
    				System.out.println("test");
    			}
    		}
    	}
    
    	public static void test5(int n) {
    		// 8 = 2^3
    		// 16 = 2^4
    		
    		// 3 = log2(8)
    		// 4 = log2(16)
    		
    		// 执行次数 = log2(n)
    		// O(logn)
    		while ((n = n / 2) > 0) {
    			System.out.println("test");
    		}
    	}
    
    	public static void test6(int n) {
    		// log5(n)
    		// O(logn)
    		while ((n = n / 5) > 0) {
    			System.out.println("test");
    		}
    	}
    
    	public static void test7(int n) {
    		// 1 + 2*log2(n) + log2(n) * (1 + 3n)
    		
    		// 1 + 3*log2(n) + 2 * nlog2(n)
    		// O(nlogn)
    		for (int i = 1; i < n; i = i * 2) {
    			// 1 + 3n
    			for (int j = 0; j < n; j++) {
    				System.out.println("test");
    			}
    		}
    	}
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
  • 相关阅读:
    P13 JDBC 简介
    倍福PLC通过程序获取系统时间、本地时间、当前时区以及系统时间时区转换
    Eureka Server 实现在线扩容
    GEN 自动生成 GORM 模型结构体文件及使用示例
    自定义RunTimeException工具类
    数组c++介绍
    神经网络基础
    快速上手SpringBoot
    一生一芯18——Chisel模板与Chisel工程构建
    初识 Linux Shell
  • 原文地址:https://blog.csdn.net/u013400314/article/details/133317767