摘要: 方法一、递归: 找到递归关系,以及终止条件 方法二、递归 + 记忆 (dp 解法),注意 因为要比较每个TreeNode的是否相同(地址和数值都要一样,即TreeNode本身),所以 一定不要重写(没必要)自定义的类TreeNode中的equals和hashcode方法。 方法三、为了解决方法一重复 阅读全文
posted @ 2019-09-08 20:01 lasclocker 阅读(168) 评论(0) 推荐(0) 编辑
摘要: dp 解法 / 转化为 Max(rob(nums[0:n 1]), rob(nums[1:n])) / public int rob(int[] nums) { if (nums.length == 0) { return 0; } if (nums.length == 1) { return nu 阅读全文
posted @ 2019-09-06 23:09 lasclocker 阅读(141) 评论(0) 推荐(0) 编辑
摘要: 解法: dp 问题 Java 5行代码 public int rob(int[] nums) { int[] dp = new int[nums.length + 2]; for (int i = 0; i 阅读全文
posted @ 2019-09-05 23:04 lasclocker 阅读(167) 评论(0) 推荐(0) 编辑
摘要: dp 解法(类似 bfs, 有些像杨辉三角) dfs 解法(递归回溯+剪枝) 阅读全文
posted @ 2019-09-01 22:17 lasclocker 阅读(724) 评论(0) 推荐(0) 编辑
摘要: dp 时间复杂度O(N), 空间复杂度O(N) dp 优化 时间复杂度O(N), 空间复杂度O(1) 阅读全文
posted @ 2019-09-01 18:15 lasclocker 阅读(175) 评论(0) 推荐(0) 编辑
摘要: dp 时间复杂度O(N^2) dp + 二分查找 时间复杂度O(N lg(N)) public int lengthOfLIS(int[] nums) { int[] smallestTails = new int[nums.length]; int realSize = 0; for (int i 阅读全文
posted @ 2019-09-01 16:42 lasclocker 阅读(147) 评论(0) 推荐(0) 编辑
摘要: dp 解法 时间复杂度 O(N^2) public int findNumberOfLIS(int[] nums) { int[] length = new int[nums.length]; int[] count = new int[nums.length]; for (int i = 0; i 阅读全文
posted @ 2019-09-01 12:48 lasclocker 阅读(458) 评论(0) 推荐(0) 编辑
摘要: 参考: https://leetcode cn.com/problems/word break/solution/dan ci chai fen by leetcode/ 暴力递归 超时,时间复杂度:O(N^N), 空间复杂度O(N): 递归的深度 BFS + 记忆,accept dp 动态规划 d 阅读全文
posted @ 2019-08-25 23:40 lasclocker 阅读(331) 评论(0) 推荐(0) 编辑
摘要: public int numTilings(int N) { // dp转移方程: dp(n) = 2 dp(n 1) + dp(n 3), 时间复杂度O(N), 空间复杂度O(1) if (N == 0) { return 0; } if (N == 1) { return 1; } if (N 阅读全文
posted @ 2019-08-24 19:51 lasclocker 阅读(403) 评论(0) 推荐(0) 编辑
摘要: 解法一:暴力穷举 时间复杂度 (m n)^2, 空间复杂度 (1) 解法三:dp 递推公式: dp[j] = min(dp[j], dp[j 1], pre_dp[j 1]) + 1, 时间复杂度 (m n), 空间复杂度 (n) 阅读全文
posted @ 2019-08-22 07:53 lasclocker 阅读(104) 评论(0) 推荐(0) 编辑