随笔分类 - LeetCode
摘要:贪心 import java.util.Arrays; import java.util.Comparator; import java.util.LinkedList; class Solution { public int[][] merge(int[][] intervals) { /** *
阅读全文
摘要:贪心 import java.util.LinkedList; import java.util.List; class Solution { public List<Integer> partitionLabels(String s) { /** * 用哈希表记录每个字母出现的最后位置 */ in
阅读全文
摘要:贪心 import java.util.Arrays; import java.util.Comparator; class Solution { public int eraseOverlapIntervals(int[][] intervals) { /** * 和《452. 用最少数量的箭引爆
阅读全文
摘要:贪心 import java.util.Arrays; import java.util.Comparator; import java.util.LinkedList; class Solution { public int findMinArrowShots(int[][] points) {
阅读全文
摘要:贪心 import java.util.Arrays; import java.util.Comparator; import java.util.LinkedList; class Solution { public int[][] reconstructQueue(int[][] people)
阅读全文
摘要:贪心 class Solution { public boolean lemonadeChange(int[] bills) { int five = 0; int ten = 0; for (int i = 0; i < bills.length; i++) { if (bills[i] == 5
阅读全文
摘要:贪心 import java.util.Arrays; class Solution { public int candy(int[] ratings) { /** * 初始每个孩子一颗糖 */ int[] res = new int[ratings.length]; Arrays.fill(res
阅读全文
摘要:贪心 import java.util.Arrays; class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { /** * 如果总的油量小于总的消耗量,那肯定无法跑一圈 */ if (Arrays.stream(
阅读全文
摘要:贪心 import java.util.Arrays; class Solution { public int largestSumAfterKNegations(int[] nums, int k) { /** * 先将数组排序,让负数在前面,然后只反转负数 */ Arrays.sort(nums
阅读全文
摘要:贪心 class Solution { public int jump(int[] nums) { /** * 如果只有一个元素,那不用走 */ if (nums.length == 1){ return 0; } /** * curMax记录在第i个元素能走到的最远距离 * max记录在curMa
阅读全文
摘要:贪心 class Solution { public boolean canJump(int[] nums) { if (nums.length == 1){ return true; } int max= 0; /** * 遍历数组,实时更新所能达到的最大距离 * 如果遇到nums[i] == 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. 买
阅读全文
摘要:贪心 class Solution { public int maxSubArray(int[] nums) { /** * 元素中有负数,因此max初始值取负数最小值 */ int max = -Integer.MAX_VALUE; int sum = 0; for (int i = 0; i <
阅读全文
摘要:中序遍历 class Solution { int sum = 0; public TreeNode convertBST(TreeNode root) { inorder(root); return root; } /** * 按照右中左的顺序进行中序遍历 * 对中间的节点进行赋值 */ publ
阅读全文
摘要:深度优先搜索 class Solution { public TreeNode trimBST(TreeNode root, int low, int high) { if (root == null){ return root; } /** * 如果根节点小于最小值,那其左子树肯定也小于最小值,那
阅读全文
摘要:深度优先搜索 class Solution { public TreeNode insertIntoBST(TreeNode root, int val) { if (root == null){ return new TreeNode(val); } if (root.val > val){ ro
阅读全文
摘要:深度优先搜索 class Solution { /** * 维护一个出现频率的最大值,如果频率等于最大值时就添加这个节点 * 如果频率大于这个最大值,就清除已有的节点,更新最大值(二叉搜索树中序遍历,节点是排好序的) * 要和前一个节点比较大小,需要一个prev指针 */ ArrayList<Int
阅读全文
摘要:深度优先搜索 class Solution { int res = Integer.MAX_VALUE; TreeNode prev; public int getMinimumDifference(TreeNode root) { if (root == null){ return 0; } /*
阅读全文
摘要:深度优先搜索 class Solution { int res = Integer.MAX_VALUE; TreeNode prev; public int minDiffInBST(TreeNode root) { if (root == null){ return 0; } /** * 中序遍历
阅读全文
摘要:深度优先搜索 class Solution { public TreeNode searchBST(TreeNode root, int val) { if (root == null){ return root; } if (root.val == val){ return root; } els
阅读全文

浙公网安备 33010602011771号