02 2022 档案

摘要:动态规划 class Solution { public int findLength(int[] nums1, int[] nums2) { /** * dp[i][j]定义为nums1[i - 1] == nums2[j - 1]时的最长重复子数组的长度 * 因为i - 1必须要大于等于0,所以 阅读全文
posted @ 2022-02-28 23:03 振袖秋枫问红叶 阅读(28) 评论(0) 推荐(0)
摘要:动态规划 import java.util.Arrays; class Solution { public int findLengthOfLCIS(int[] nums) { /** * dp[i]定义为以nums[i]结尾的最长连续递增子序列 * 每个数字自己都可以构成一个序列,因此初始化长度都 阅读全文
posted @ 2022-02-28 21:58 振袖秋枫问红叶 阅读(37) 评论(0) 推荐(0)
摘要:动态规划 import java.util.Arrays; class Solution { public int lengthOfLIS(int[] nums) { /** * dp[i]定义为以nums[i]结尾的最长递增子序列 * 每个数字自己都可以构成一个序列,因此初始化长度都为1 */ i 阅读全文
posted @ 2022-02-28 21:17 振袖秋枫问红叶 阅读(39) 评论(0) 推荐(0)
摘要:动态规划 class Solution { public int maxProfit(int[] prices) { if (prices.length == 1){ return 0; } int[][] dp = new int[prices.length][4]; dp[0][0] = -pr 阅读全文
posted @ 2022-02-28 19:56 振袖秋枫问红叶 阅读(37) 评论(0) 推荐(0)
摘要:动态规划 class Solution { public int maxProfit(int k, int[] prices) { if (prices.length == 0){ return 0; } int[][] dp = new int[prices.length][2 * k + 1]; 阅读全文
posted @ 2022-02-28 17:05 振袖秋枫问红叶 阅读(36) 评论(0) 推荐(0)
摘要:动态规划 class Solution { public int maxProfit(int[] prices) { int[][] dp = new int[prices.length][5]; dp[0][0] = 0; dp[0][1] = -prices[0]; dp[0][2] = 0; 阅读全文
posted @ 2022-02-28 16:23 振袖秋枫问红叶 阅读(35) 评论(0) 推荐(0)
摘要:动态规划 class Solution { public int maxProfit(int[] prices) { /** * 因为只能买一次,因此无法确定哪天买了还有每天的持股状态,需要定义一个二维数组分别存储持不持有该股票的利润 * dp[i][0]为第i天持有该股票的利润 * dp[i][1 阅读全文
posted @ 2022-02-28 13:24 振袖秋枫问红叶 阅读(43) 评论(0) 推荐(0)
摘要:贪心 class Solution { int num = 0; public int minCameraCover(TreeNode root) { /** * 如果根节点未被监视,需要单独放一个摄像头 */ if (dfs(root) == 0){ num++; } return num; } 阅读全文
posted @ 2022-02-27 11:08 振袖秋枫问红叶 阅读(53) 评论(0) 推荐(0)
摘要:动态规划 class Solution { public int maxProfit(int[] prices, int fee) { int[][] dp = new int[prices.length][2]; dp[0][0] = -prices[0]; /** * 和《122. 买卖股票的最 阅读全文
posted @ 2022-02-27 10:22 振袖秋枫问红叶 阅读(58) 评论(0) 推荐(0)
摘要:贪心 class Solution { public int monotoneIncreasingDigits(int n) { /** * 将数字拆分为字符数组 * start为第一个需要变为9的位置,后面的位置全部要变为9 */ char[] chars = String.valueOf(n). 阅读全文
posted @ 2022-02-26 21:50 振袖秋枫问红叶 阅读(46) 评论(0) 推荐(0)
摘要:贪心 class Solution { public int wiggleMaxLength(int[] nums) { int prev = 0; int cur = 0; int count = 1; /** * 遍历时记录前一个差值和当前差值,如果差值正负交替,总数就加1,然后更新前一个差值 阅读全文
posted @ 2022-02-26 19:59 振袖秋枫问红叶 阅读(35) 评论(0) 推荐(0)
摘要:贪心 import java.util.Arrays; import java.util.Comparator; import java.util.LinkedList; class Solution { public int[][] merge(int[][] intervals) { /** * 阅读全文
posted @ 2022-02-26 17:42 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要:贪心 import java.util.LinkedList; import java.util.List; class Solution { public List<Integer> partitionLabels(String s) { /** * 用哈希表记录每个字母出现的最后位置 */ in 阅读全文
posted @ 2022-02-26 17:17 振袖秋枫问红叶 阅读(50) 评论(0) 推荐(0)
摘要:贪心 import java.util.Arrays; import java.util.Comparator; class Solution { public int eraseOverlapIntervals(int[][] intervals) { /** * 和《452. 用最少数量的箭引爆 阅读全文
posted @ 2022-02-26 15:59 振袖秋枫问红叶 阅读(32) 评论(0) 推荐(0)
摘要:贪心 import java.util.Arrays; import java.util.Comparator; import java.util.LinkedList; class Solution { public int findMinArrowShots(int[][] points) { 阅读全文
posted @ 2022-02-26 15:35 振袖秋枫问红叶 阅读(41) 评论(0) 推荐(0)
摘要:贪心 import java.util.Arrays; import java.util.Comparator; import java.util.LinkedList; class Solution { public int[][] reconstructQueue(int[][] people) 阅读全文
posted @ 2022-02-26 14:23 振袖秋枫问红叶 阅读(43) 评论(0) 推荐(0)
摘要:贪心 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 阅读全文
posted @ 2022-02-26 13:12 振袖秋枫问红叶 阅读(47) 评论(0) 推荐(0)
摘要:贪心 import java.util.Arrays; class Solution { public int candy(int[] ratings) { /** * 初始每个孩子一颗糖 */ int[] res = new int[ratings.length]; Arrays.fill(res 阅读全文
posted @ 2022-02-26 12:22 振袖秋枫问红叶 阅读(41) 评论(0) 推荐(0)
摘要:贪心 import java.util.Arrays; class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { /** * 如果总的油量小于总的消耗量,那肯定无法跑一圈 */ if (Arrays.stream( 阅读全文
posted @ 2022-02-26 11:33 振袖秋枫问红叶 阅读(45) 评论(0) 推荐(0)
摘要:贪心 import java.util.Arrays; class Solution { public int largestSumAfterKNegations(int[] nums, int k) { /** * 先将数组排序,让负数在前面,然后只反转负数 */ Arrays.sort(nums 阅读全文
posted @ 2022-02-25 17:48 振袖秋枫问红叶 阅读(28) 评论(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 振袖秋枫问红叶 阅读(43) 评论(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 振袖秋枫问红叶 阅读(21) 评论(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 振袖秋枫问红叶 阅读(39) 评论(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 振袖秋枫问红叶 阅读(22) 评论(0) 推荐(0)
摘要:中序遍历 class Solution { int sum = 0; public TreeNode convertBST(TreeNode root) { inorder(root); return root; } /** * 按照右中左的顺序进行中序遍历 * 对中间的节点进行赋值 */ publ 阅读全文
posted @ 2022-02-24 13:36 振袖秋枫问红叶 阅读(35) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public TreeNode trimBST(TreeNode root, int low, int high) { if (root == null){ return root; } /** * 如果根节点小于最小值,那其左子树肯定也小于最小值,那 阅读全文
posted @ 2022-02-24 13:13 振袖秋枫问红叶 阅读(37) 评论(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 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { /** * 维护一个出现频率的最大值,如果频率等于最大值时就添加这个节点 * 如果频率大于这个最大值,就清除已有的节点,更新最大值(二叉搜索树中序遍历,节点是排好序的) * 要和前一个节点比较大小,需要一个prev指针 */ ArrayList<Int 阅读全文
posted @ 2022-02-24 10:50 振袖秋枫问红叶 阅读(30) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { int res = Integer.MAX_VALUE; TreeNode prev; public int minDiffInBST(TreeNode root) { if (root == null){ return 0; } /** * 中序遍历 阅读全文
posted @ 2022-02-22 23:41 振袖秋枫问红叶 阅读(37) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { int res = Integer.MAX_VALUE; TreeNode prev; public int getMinimumDifference(TreeNode root) { if (root == null){ return 0; } /* 阅读全文
posted @ 2022-02-22 23:41 振袖秋枫问红叶 阅读(49) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public TreeNode searchBST(TreeNode root, int val) { if (root == null){ return root; } if (root.val == val){ return root; } els 阅读全文
posted @ 2022-02-22 22:11 振袖秋枫问红叶 阅读(26) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { /** * 如果某棵树为空,则返回另一棵树 */ if (root1 == null){ return root2; } if ( 阅读全文
posted @ 2022-02-22 21:51 振袖秋枫问红叶 阅读(37) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public TreeNode constructMaximumBinaryTree(int[] nums) { return buildSonTree(nums, 0, nums.length); } /** * 左闭右开区间 * 先找到数组的最大值 阅读全文
posted @ 2022-02-22 21:27 振袖秋枫问红叶 阅读(35) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { return buildSonTree(preorder, 0, preorder.length, inorder, 0, inord 阅读全文
posted @ 2022-02-22 21:14 振袖秋枫问红叶 阅读(47) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { return buildSonTree(inorder, 0, inorder.length, postorder, 0, post 阅读全文
posted @ 2022-02-22 20:50 振袖秋枫问红叶 阅读(49) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public int findBottomLeftValue(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); int res = 0; queue.add(root); whil 阅读全文
posted @ 2022-02-22 16:06 振袖秋枫问红叶 阅读(27) 评论(0) 推荐(0)
摘要:深度优先搜索 /** * 时间复杂度 O(n) * 空间复杂度 O(logn) */class Solution { public int maxDepth(Node root) { if (root == null){ return 0; } int max = 0; for (Node c : 阅读全文
posted @ 2022-02-21 17:54 振袖秋枫问红叶 阅读(31) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public boolean isSubtree(TreeNode root, TreeNode subRoot) { /** * 对于树的每个节点,都判断一下是否和子树是一个树(《100. 相同的树》) */ if (subRoot == null) 阅读全文
posted @ 2022-02-21 17:11 振袖秋枫问红叶 阅读(31) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { List<Integer> list = new LinkedList<>(); public List<Integer> postorder(Node root) { if (root == null){ return list; } for (No 阅读全文
posted @ 2022-02-21 14:52 振袖秋枫问红叶 阅读(23) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { List<Integer> list = new LinkedList<>(); public List<Integer> preorder(Node root) { if (root == null){ return list; } list.add 阅读全文
posted @ 2022-02-21 14:40 振袖秋枫问红叶 阅读(23) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public Node connect(Node root) { Queue<Node> queue = new LinkedList<>(); if (root == null){ return root; } queue.add(root); wh 阅读全文
posted @ 2022-02-21 13:54 振袖秋枫问红叶 阅读(32) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public Node connect(Node root) { Queue<Node> queue = new LinkedList<>(); if (root == null){ return root; } queue.add(root); wh 阅读全文
posted @ 2022-02-21 13:50 振袖秋枫问红叶 阅读(27) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public List<Integer> largestValues(TreeNode root) { LinkedList<Integer> list = new LinkedList<>(); Queue<TreeNode> queue = new 阅读全文
posted @ 2022-02-21 13:05 振袖秋枫问红叶 阅读(37) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public List<List<Integer>> levelOrder(Node root) { List<List<Integer>> list = new LinkedList<>(); Queue<Node> queue = new Link 阅读全文
posted @ 2022-02-21 12:05 振袖秋枫问红叶 阅读(34) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public List<Double> averageOfLevels(TreeNode root) { List<Double> list = new LinkedList<>(); Queue<TreeNode> queue = new Linke 阅读全文
posted @ 2022-02-21 11:46 振袖秋枫问红叶 阅读(33) 评论(0) 推荐(0)
摘要:双端队列实现单调队列 import java.util.ArrayDeque; class Solution { public int[] maxSlidingWindow(int[] nums, int k) { /** * 双端队列实现单调队列 * 队列存储的是元素的索引 */ Deque<In 阅读全文
posted @ 2022-02-20 11:26 振袖秋枫问红叶 阅读(40) 评论(0) 推荐(0)
摘要:栈 import java.util.Stack; class Solution { public String removeDuplicates(String s) { Stack<Character> stack = new Stack(); for (int i = 0; i < s.leng 阅读全文
posted @ 2022-02-19 22:48 振袖秋枫问红叶 阅读(65) 评论(0) 推荐(0)
摘要:class Solution { public boolean repeatedSubstringPattern(String s) { /** * 将一个字符串复制两份,然后去掉首尾元素,如果剩余的子串中还包含原字符串,说明原字符串含有重复的子串 */ String str = s + s; re 阅读全文
posted @ 2022-02-19 20:57 振袖秋枫问红叶 阅读(39) 评论(0) 推荐(0)
摘要:双指针 class Solution { public int strStr(String haystack, String needle) { /** * 如果needle为空,结果肯定为0 * 如果needle不为空,如果haystack为空,结果肯定为-1 */ if (needle.leng 阅读全文
posted @ 2022-02-19 20:44 振袖秋枫问红叶 阅读(28) 评论(0) 推荐(0)
摘要:双指针 class Solution { public String reverseLeftWords(String s, int n) { StringBuilder str = new StringBuilder(s); /** * 分别反转前n个字符和剩余字符,然后反转全部字符 */ reve 阅读全文
posted @ 2022-02-19 20:05 振袖秋枫问红叶 阅读(33) 评论(0) 推荐(0)
摘要:双指针 class Solution { public String reverseWords(String s) { StringBuilder str = new StringBuilder(); int left = 0; int right = s.length() - 1; /** * 去 阅读全文
posted @ 2022-02-19 19:43 振袖秋枫问红叶 阅读(31) 评论(0) 推荐(0)
摘要:class Solution { public String replaceSpace(String s) { StringBuilder str = new StringBuilder(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i 阅读全文
posted @ 2022-02-19 18:34 振袖秋枫问红叶 阅读(26) 评论(0) 推荐(0)
摘要:双指针 class Solution { public String reverseStr(String s, int k) { char[] chars = s.toCharArray(); /** * 循环的间隔设置为2k,如果最后的区间小于k,全部反转;大于k小于2k则反转前k个 * 每次右边 阅读全文
posted @ 2022-02-19 18:25 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要:哈希表 class Solution { public boolean canConstruct(String ransomNote, String magazine) { int[] arr = new int[128]; for (int i = 0; i < magazine.length() 阅读全文
posted @ 2022-02-18 17:29 振袖秋枫问红叶 阅读(33) 评论(0) 推荐(0)
摘要:双指针法 class Solution { public boolean hasCycle(ListNode head) { /** * 判断是否存在环形链表:创建快慢指针,从头节点出发,如果两个指针能相遇,说明存在环形链表 */ ListNode fast = head; ListNode slo 阅读全文
posted @ 2022-02-18 13:29 振袖秋枫问红叶 阅读(28) 评论(0) 推荐(0)
摘要:双指针法 class Solution { public ListNode detectCycle(ListNode head) { /** * 1、判断是否存在环形链表:创建快慢指针,从头节点出发,如果两个指针能相遇,说明存在环形链表 * 2、找到环形入口:在链表头节点和快慢指针相遇的节点分别创建 阅读全文
posted @ 2022-02-18 13:22 振袖秋枫问红叶 阅读(33) 评论(0) 推荐(0)
摘要:class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode h1 = headA; ListNode h2 = headB; /** * 两个长度不同的链表,如果同步遍 阅读全文
posted @ 2022-02-18 11:24 振袖秋枫问红叶 阅读(33) 评论(0) 推荐(0)
摘要:class MyLinkedList { int size; ListNode dummyHead; public MyLinkedList() { size = 0; dummyHead = new ListNode(-1); } /** * 定义一个获取任意索引前一个节点的方法 */ publi 阅读全文
posted @ 2022-02-18 09:33 振袖秋枫问红叶 阅读(30) 评论(0) 推荐(0)
摘要:模拟 class Solution { public int[] spiralOrder(int[][] matrix) { if (matrix.length == 0){ return new int[0]; } int m = matrix.length; int n = matrix[0]. 阅读全文
posted @ 2022-02-17 16:29 振袖秋枫问红叶 阅读(28) 评论(0) 推荐(0)
摘要:模拟 import java.util.ArrayList; import java.util.List; class Solution { public List<Integer> spiralOrder(int[][] matrix) { /** * 定义上下左右边界,每次循环一圈后都要缩小边界 阅读全文
posted @ 2022-02-17 16:14 振袖秋枫问红叶 阅读(28) 评论(0) 推荐(0)
摘要:模拟 class Solution { public int[][] generateMatrix(int n) { /** * 定义上下左右边界,每次循环一圈后都要缩小边界 * 从1开始赋值 */ int[][] arr = new int[n][n]; int top = 0; int bott 阅读全文
posted @ 2022-02-17 15:50 振袖秋枫问红叶 阅读(25) 评论(0) 推荐(0)
摘要:滑动窗口法 class Solution { public int totalFruit(int[] fruits) { /** * 滑动窗口 * 最多只能存在两种数字,right从1开始 * i和j记录最先遇到的两种数字,一旦发现第三种数字,就记录当前的长度 */ int left = 0; in 阅读全文
posted @ 2022-02-17 13:46 振袖秋枫问红叶 阅读(37) 评论(0) 推荐(0)
摘要:双指针法 class Solution { public int[] sortedSquares(int[] nums) { /** * 先求出所有元素的平方,然后从两端双指针遍历 * 两端的元素总是最大的,逆序插入新的数组 */ for (int i = 0; i < nums.length; i 阅读全文
posted @ 2022-02-17 10:57 振袖秋枫问红叶 阅读(39) 评论(0) 推荐(0)
摘要:栈 import java.util.Stack; class Solution { public boolean backspaceCompare(String s, String t) { /** * 将字符串压入栈,遇到#则弹出 */ Stack<Character> stack1 = new 阅读全文
posted @ 2022-02-17 09:57 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要:二分查找 class Solution { public boolean isPerfectSquare(int num) { int left = 1; int right = num; while (left <= right){ int mid = left + (right - left) 阅读全文
posted @ 2022-02-16 21:28 振袖秋枫问红叶 阅读(40) 评论(0) 推荐(0)
摘要:二分查找 class Solution { public int mySqrt(int x) { int left = 0; int right = x; while (left <= right){ int mid = left + (right - left) / 2; /** * 避免整形溢出 阅读全文
posted @ 2022-02-16 21:22 振袖秋枫问红叶 阅读(21) 评论(0) 推荐(0)
摘要:两个二分查找分别寻找 class Solution { public int[] searchRange(int[] nums, int target) { /** * 分别寻找第一个和最后一个target */ int left = 0; int right = nums.length - 1; 阅读全文
posted @ 2022-02-16 20:30 振袖秋枫问红叶 阅读(34) 评论(0) 推荐(0)
摘要:二分查找 class Solution { public int searchInsert(int[] nums, int target) { /** * 左闭右闭写法,left可以等于right */ int left = 0; int right = nums.length - 1; while 阅读全文
posted @ 2022-02-16 19:35 振袖秋枫问红叶 阅读(42) 评论(0) 推荐(0)