1、面试中的复杂度分析

内容来自刘宇波老师玩转算法面试

1、一个时间复杂度的问题

image
image

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

image

3、空间复杂度

image

4、简单的复杂度分析

public class Test {

    /**
     * O(1)
     */
    private static void swap(char[] chars, int a, int b) {
        char k = chars[a];
        chars[a] = chars[b];
        chars[b] = k;
    }

    /**
     * O(N)
     */
    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);
    }

    /**
     * O(logN)
     */
    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;
    }

    /**
     * O(sqrt(N))
     * 数学上指在大于 1 的整数中, 只能被 1 和它本身整除的数
     */
    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、递归中进行一次递归调用的复杂度分析

image
image
image
image

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

image
image
image

6、均摊复杂度分析

动态数组(Java 实现)

template<typename T>
class MyVector {

private:
    T *data;
    int size;       // 存储数组中的元素个数
    int capacity;   // 存储数组中可以容纳的最大的元素个数

    /**
     * 复杂度为 O(N)
     */
    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;
    }

    /**
     * 平均复杂度为 O(1)
     */
    void push_back(T e) {
        if (size == capacity) resize(2 * capacity);
        data[size++] = e;
    }

    /**
     * 平均复杂度为 O(1)
     */
    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
posted @ 2023-04-26 11:36  lidongdongdong~  阅读(16)  评论(0编辑  收藏  举报