上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 36 下一页
摘要: 贪心 import java.util.Arrays; class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { /** * 如果总的油量小于总的消耗量,那肯定无法跑一圈 */ if (Arrays.stream( 阅读全文
posted @ 2022-02-26 11:33 振袖秋枫问红叶 阅读(34) 评论(0) 推荐(0)
摘要: 贪心 import java.util.Arrays; class Solution { public int largestSumAfterKNegations(int[] nums, int k) { /** * 先将数组排序,让负数在前面,然后只反转负数 */ Arrays.sort(nums 阅读全文
posted @ 2022-02-25 17:48 振袖秋枫问红叶 阅读(22) 评论(0) 推荐(0)
摘要: 贪心 class Solution { public int jump(int[] nums) { /** * 如果只有一个元素,那不用走 */ if (nums.length == 1){ return 0; } /** * curMax记录在第i个元素能走到的最远距离 * max记录在curMa 阅读全文
posted @ 2022-02-25 17:16 振袖秋枫问红叶 阅读(34) 评论(0) 推荐(0)
摘要: 贪心 class Solution { public boolean canJump(int[] nums) { if (nums.length == 1){ return true; } int max= 0; /** * 遍历数组,实时更新所能达到的最大距离 * 如果遇到nums[i] == 0 阅读全文
posted @ 2022-02-25 10:57 振袖秋枫问红叶 阅读(10) 评论(0) 推荐(0)
摘要: 动态规划 class Solution { public int maxProfit(int[] prices) { int[][] dp = new int[prices.length][2]; dp[0][0] = -prices[0]; dp[0][1] = 0; /** * 和《121. 买 阅读全文
posted @ 2022-02-25 10:25 振袖秋枫问红叶 阅读(31) 评论(0) 推荐(0)
摘要: 贪心 class Solution { public int maxSubArray(int[] nums) { /** * 元素中有负数,因此max初始值取负数最小值 */ int max = -Integer.MAX_VALUE; int sum = 0; for (int i = 0; i < 阅读全文
posted @ 2022-02-25 09:55 振袖秋枫问红叶 阅读(21) 评论(0) 推荐(0)
摘要: 中序遍历 class Solution { int sum = 0; public TreeNode convertBST(TreeNode root) { inorder(root); return root; } /** * 按照右中左的顺序进行中序遍历 * 对中间的节点进行赋值 */ publ 阅读全文
posted @ 2022-02-24 13:36 振袖秋枫问红叶 阅读(27) 评论(0) 推荐(0)
摘要: 深度优先搜索 class Solution { public TreeNode trimBST(TreeNode root, int low, int high) { if (root == null){ return root; } /** * 如果根节点小于最小值,那其左子树肯定也小于最小值,那 阅读全文
posted @ 2022-02-24 13:13 振袖秋枫问红叶 阅读(29) 评论(0) 推荐(0)
摘要: 深度优先搜索 class Solution { public TreeNode insertIntoBST(TreeNode root, int val) { if (root == null){ return new TreeNode(val); } if (root.val > val){ ro 阅读全文
posted @ 2022-02-24 11:42 振袖秋枫问红叶 阅读(35) 评论(0) 推荐(0)
摘要: 深度优先搜索 class Solution { /** * 维护一个出现频率的最大值,如果频率等于最大值时就添加这个节点 * 如果频率大于这个最大值,就清除已有的节点,更新最大值(二叉搜索树中序遍历,节点是排好序的) * 要和前一个节点比较大小,需要一个prev指针 */ ArrayList<Int 阅读全文
posted @ 2022-02-24 10:50 振袖秋枫问红叶 阅读(24) 评论(0) 推荐(0)
上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 36 下一页