内容来自刘宇波老师玩转算法面试
1、一个时间复杂度的问题


2、数据规模的概念
| private static void testDataSize() { |
| for (int x = 1; x <= 9; x++) { |
| int n = (int) Math.pow(10, x); |
| |
| long startTime = System.nanoTime(); |
| int sum = 0; |
| for (int i = 0; i < n; i++) sum += i; |
| long endTime = System.nanoTime(); |
| |
| double time = (endTime - startTime) / 1000000000.0; |
| System.out.println(String.format("10 ^ %d : %f s", x, time)); |
| } |
| } |
| 10 ^ 1 : 0.000001 s |
| 10 ^ 2 : 0.000001 s |
| 10 ^ 3 : 0.000005 s |
| 10 ^ 4 : 0.000052 s |
| 10 ^ 5 : 0.000512 s |
| 10 ^ 6 : 0.001563 s |
| 10 ^ 7 : 0.006768 s |
| 10 ^ 8 : 0.024428 s |
| 10 ^ 9 : 0.244828 s |

3、空间复杂度

4、简单的复杂度分析
| public class Test { |
| |
| |
| |
| |
| private static void swap(char[] chars, int a, int b) { |
| char k = chars[a]; |
| chars[a] = chars[b]; |
| chars[b] = k; |
| } |
| |
| |
| |
| |
| private static String revsrse(String s) { |
| if (s.length() == 0) return ""; |
| |
| char[] chars = s.toCharArray(); |
| for (int i = 0; i < chars.length / 2; i++) { |
| swap(chars, i, chars.length - 1 - i); |
| } |
| |
| return new String(chars); |
| } |
| |
| |
| |
| |
| private static String intToString(int num) { |
| if (num == 0) return "0"; |
| |
| boolean isNegative = false; |
| if (num < 0) { |
| isNegative = true; |
| num = -num; |
| } |
| |
| StringBuilder sb = new StringBuilder(); |
| while (num != 0) { |
| sb.append((char) ('0' + num % 10)); |
| num /= 10; |
| } |
| String str = revsrse(sb.toString()); |
| if (isNegative) str = "-" + str; |
| |
| return str; |
| } |
| |
| |
| |
| |
| |
| private static boolean isPrime(int num) { |
| if (num <= 1) return false; |
| for (int x = 2; x * x <= num; x++) { |
| if (num % x == 0) return false; |
| } |
| return true; |
| } |
| |
| public static void main(String[] args) { |
| System.out.println(intToString(0)); |
| System.out.println(intToString(100)); |
| System.out.println(intToString(-100)); |
| |
| System.out.println(isPrime(99991)); |
| } |
| } |
5、递归函数的复杂度分析
5.1、递归中进行一次递归调用的复杂度分析




5.2、递归中进行多次递归调用



6、均摊复杂度分析
动态数组(Java 实现)
| template<typename T> |
| class MyVector { |
| |
| private: |
| T *data; |
| int size; |
| int capacity; |
| |
| |
| |
| |
| void resize(int newCapacity) { |
| assert(newCapacity >= size); |
| |
| T *newData = new T[newCapacity]; |
| for (int i = 0; i < size; ++i) newData[i] = data[i]; |
| delete[] data; |
| data = newData; |
| capacity = newCapacity; |
| } |
| |
| public: |
| MyVector() { |
| data = new T[100]; |
| size = 0; |
| capacity = 100; |
| } |
| |
| ~MyVector() { |
| delete[] data; |
| } |
| |
| |
| |
| |
| void push_back(T e) { |
| if (size == capacity) resize(2 * capacity); |
| data[size++] = e; |
| } |
| |
| |
| |
| |
| T pop_back() { |
| assert(size > 0); |
| T ret = data[--size]; |
| |
| |
| if (size == capacity / 4 && capacity / 2 != 0) resize(capacity / 2); |
| |
| return ret; |
| } |
| }; |
| #include <iostream> |
| #include <cassert> |
| #include <cmath> |
| #include <ctime> |
| #include "MyVector.h" |
| |
| using namespace std; |
| |
| int main() { |
| |
| for (int i = 10; i <= 26; i++) { |
| int n = pow(2, i); |
| |
| MyVector<int> vec; |
| clock_t startTime = clock(); |
| for (int num = 0; num < n; num++) vec.push_back(i); |
| for (int num = 0; num < n; num++) vec.pop_back(); |
| clock_t endTime = clock(); |
| |
| cout << 2 * n << " operations: \t"; |
| cout << double(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl; |
| } |
| |
| return 0; |
| } |
7、Java 中的基本数据类型
数据类型 |
关键字 |
内存占用 |
取值范围 |
整数 |
byte |
1 Byte |
- 2 ^ 7 ~ 2 ^ 7 - 1 |
|
short |
2 Byte |
- 2 ^ 15 ~ 2 ^ 15 - 1 |
|
int |
4 Byte |
- 2 ^ 31 ~ 2 ^ 31 - 1 |
|
long |
8 Byte |
- 2 ^ 63 ~ 2 ^ 63 - 1 |
浮点数 |
float |
4 Byte |
1.401298 × 10 ^ -45 ~ 3.402823 × 10 ^ 38 |
|
double |
8 Byte |
4.9000000 × 10 ^ -324^ ~ 1.797693 × 10 ^ 308 |
布尔 |
boolean |
1 Byte |
true,false |
字符 |
char |
2 Byte |
0 - 65535 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步