11 2022 档案
摘要:今天是第四十九天,用动态规划的思想去解决之前用贪心算法解决的股票买卖问题 121. 买卖股票的最佳时机 class Solution { public int maxProfit(int[] prices) { int n = prices.length; if(n == 1){ return 0;
阅读全文
摘要:今天是训练营的第四十八天,开始了动态规划的强盗问题 198. 打家劫舍 class Solution { public int rob(int[] nums) { int n = nums.length; if(n==1){ return nums[0]; } int[] dp = new int[
阅读全文
摘要:今天是第四十六天,动态规划周的最后一天 139. 单词拆分 class Solution { public boolean wordBreak(String s, List<String> wordDict) { int n = s.length(); boolean[] dp = new bool
阅读全文
摘要:今天是第四十五天,继续动态规划 70. 爬楼梯 (进阶) class Solution { public int climbStairs(int n) { int[] dp = new int[n+1]; dp[0] = 1; for(int i = 0; i<=n; i++){ for(int j
阅读全文
摘要:今天是第四十四天,继续动态规划 518. 零钱兑换 II class Solution { public int change(int amount, int[] coins) { int n = coins.length; int[] dp = new int[amount+1]; dp[0] =
阅读全文
摘要:今天是第四十三天,还是背包问题 1049. 最后一块石头的重量 II class Solution { public int lastStoneWeightII(int[] stones) { int n = stones.length; int sum = 0; for (int i : ston
阅读全文
摘要:今天是第四十二天,是动态规划中的背包问题 416. 分割等和子集 class Solution { public boolean canPartition(int[] nums) { int sum = 0; int n = nums.length; int[] dp = new int[10001
阅读全文
摘要:今天是四十一天,从今天起难度大概就要上来了 343.整数拆分 class Solution { public int integerBreak(int n) { int[] dp = new int[n+1]; dp[2] = 1; for(int i = 3; i<=n; i++){ for(in
阅读全文
摘要:今天是第三十九天,只有两道动态规划的题 62.不同路径 class Solution { public int uniquePaths(int m, int n) { int[][] path = new int[m][n]; for(int i = 0; i<m; i++){ path[i][0]
阅读全文
摘要:今天是第三十八天,最难的动态规划要开始了 509. 斐波那契数 class Solution { public int fib(int n) { if(n < 2){ return n; }int[] fibNum = new int[n+1]; fibNum[0] = 0; fibNum[1] =
阅读全文
摘要:今天是第三十七天,贪心算法的最后一天 738.单调递增的数字 class Solution { public int monotoneIncreasingDigits(int n) { String num = Integer.toString(n); char[] nums = num.toCha
阅读全文
摘要:今天继续是贪心算法 435. 无重叠区间 class Solution { public int eraseOverlapIntervals(int[][] intervals) { int n = intervals.length; Arrays.sort(intervals, (a, b) ->
阅读全文
摘要:第三十五天,继续贪心 860.柠檬水找零 class Solution { public boolean lemonadeChange(int[] bills) { int n = bills.length; if(bills[0] > 5){ return false; } int five =
阅读全文
摘要:今天继续贪心算法,重点是学习贪心算法的思维 1005.K次取反后最大化的数组和 class Solution { public int largestSumAfterKNegations(int[] nums, int k) { int n = nums.length; Arrays.sort(nu
阅读全文
摘要:本来这是第三十一天的内容,但是三十一天的时候写成第三十二天的了,因此今天写第三十一天的内容 455.分发饼干 class Solution { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort
阅读全文
摘要:贪心算法的核心思想是在每一步决策中都找到局部最优解 122. 买卖股票的最佳时机 class Solution { public int maxProfit(int[] prices) { int n = prices.length; int profit = 0; int price = pric
阅读全文
摘要:今天是第二十八天,依旧是回溯 491. 递增子序列 class Solution { private List<Integer> path = new ArrayList<>(); private List<List<Integer>> res = new ArrayList<>(); public
阅读全文
摘要:今天是第二十八天,前面几天的回溯算法不太熟,今天继续巩固 ● 93.复原IP地址 class Solution { List<String> ret = new ArrayList<>(); public List<String> restoreIpAddresses(String s) { Lin
阅读全文
摘要:第二十七天,继续回溯算法 39. 组合总和 class Solution { List<Integer> list = new ArrayList<>(); List<List<Integer>> ans = new ArrayList<>(); int target; int len; publi
阅读全文
摘要:今天是第二十五天,回溯算法 216. 组合总和III class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> paths = new ArrayList<>(); int sum = 0; public
阅读全文
摘要:今天结束了二叉树的学习, 开始新的一章了 77.组合 class Solution { List<List<Integer>> res = new ArrayList<List<Integer>>(); List<Integer> list = new ArrayList<Integer>(); p
阅读全文
摘要:今天终于是 二叉树的最后一章了,三道题,加油! 669. 修剪二叉搜索树 class Solution { public TreeNode trimBST(TreeNode root, int low, int high) { if(root == null){ return root; } roo
阅读全文
摘要:今天是第22天,依旧还是二叉树 235. 二叉树的最近公共祖先 class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root == null){ retur
阅读全文
摘要:今天是第二十一天,还是二叉树,做了得有一周的二叉树了 530.二叉搜索树的最小绝对差 class Solution { int res = Integer.MAX_VALUE; TreeNode pre = null; public int getMinimumDifference(TreeNode
阅读全文