java - 如何计算此代码的时间复杂度?
问题描述
我正在努力计算这段代码中的时间复杂度。目前只能编写简单的代码……只想尝试复杂的代码!
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;
}
解决方案
基本上你可以计算分配和操作。有一个
int assignments = 0;
int operations = 0;
每次你做一个你都会增加它。
这样做的其他方法是监视时间,但这不是最可靠的方法。
您还可以计算/近似 Big-O,检查Big O,您如何计算/近似它?
推荐阅读
- php - 在循环中过滤产品集合并检索库存数量
- ms-access - MSACCESS - 如何为控件创建自定义属性
- spring-boot - Spring-boot + Rest Controller + Post Request + Kotlin + 单元测试有一个奇怪的问题
- python - 如何在不向源发送请求的情况下响应观众?
- python - 程序在 break 语句后继续循环
- javascript - 如何设置与 IAM 关联的 AWS 密钥以限制 aws-sdk 客户端可用的权限?
- java - 如何使用 Selenium 实现键盘自动化
- c++ - 运行时检查失败 #4 - 此函数保留的 _alloca 内存周围的堆栈区域已损坏?
- asp.net-mvc - 向 InsertionMode 枚举添加附加值
- ios - Swift核心数据NSFetchRequest谓词获取日期时间