首页 > 解决方案 > 如何计算此代码的时间复杂度?

问题描述

我正在努力计算这段代码中的时间复杂度。目前只能编写简单的代码……只想尝试复杂的代码!

public static int PATHWAY = 0;
public static int WALL = 1;
public static int MARKED = 2;

public static boolean find(int x, int y) {
    if(x == 7 && y == 7) return true;
    maze[x][y] = MARKED;
    if(x != 0 && maze[x-1][y] == PATHWAY && find(x-1, y)) return true;
    if(y != 0 && maze[x][y-1] == PATHWAY && find(x, y-1)) return true;
    if(x != 7 && maze[x+1][y] == PATHWAY && find(x+1, y)) return true;
    if(y != 7 && maze[x][y+1] == PATHWAY && find(x, y+1)) return true;
    return false;
}

标签: javatimecomplexity-theory

解决方案


基本上你可以计算分配和操作。有一个

int assignments = 0;
int operations = 0;

每次你做一个你都会增加它。

这样做的其他方法是监视时间,但这不是最可靠的方法。

您还可以计算/近似 Big-O,检查Big O,您如何计算/近似它?


推荐阅读