摘要: 方法三:迭代二分查找 public int findPeakElement(int[] nums) { //第二种方法不用左右加个边界 return helper(nums, 0, nums.length-1); } public int helper(int[] nums, int left, i 阅读全文
posted @ 2021-04-14 16:58 kpwong 阅读(40) 评论(0) 推荐(0) 编辑
摘要: /* * 15. 3Sum * 题意:找出数组中所有和为0的三元组合 * 难度:Medium * 分类:Array, Two Pointers * 注意:如何避免 List 重复元素 * Tips:lc15, lc16, lc923 */ import java.util.*; public cla 阅读全文
posted @ 2021-04-14 16:53 kpwong 阅读(50) 评论(0) 推荐(0) 编辑
摘要: /* * 146. LRU Cache * 题意:首先理解LRU思想,最久未被访问过的,最先被替换 * 难度:Hard * 分类:Design * 思路:hashmap + 双向链表。hashmap实现了O(1)的get,双向链表实现O(1)的put * Tips:能想到双向链表,就不难了 * lc 阅读全文
posted @ 2021-04-14 16:48 kpwong 阅读(45) 评论(0) 推荐(0) 编辑
摘要: import java.util.regex.Pattern; class Solution { String chunkIPv4 = "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"; Pattern pattenIPv4 = Pattern 阅读全文
posted @ 2021-04-14 16:29 kpwong 阅读(49) 评论(0) 推荐(0) 编辑
摘要: package code; /* * 141. Linked List Cycle * 题意:链表是否有环 * 难度:Easy * 分类:Linked List, Two Pointers * 思路:快慢指针 * lc142 */ public class lc141 { public class 阅读全文
posted @ 2021-04-14 16:27 kpwong 阅读(28) 评论(0) 推荐(0) 编辑
摘要: /* * 543. Diameter of Binary Tree * 题意:树中的最长路径 * 难度:Easy * 分类:Tree * 思路:和lc124思路一样,但lc124是Hard,这道竟然是Easy,哈哈哈 * Tips: */ public class lc543 { public cl 阅读全文
posted @ 2021-04-14 15:56 kpwong 阅读(35) 评论(0) 推荐(0) 编辑
摘要: 方法一:排序首先将数组排序。 如果数组中全是非负数,则排序后最大的三个数相乘即为最大乘积;如果全是非正数,则最大的三个数相乘同样也为最大乘积。 如果数组中有正数有负数,则最大乘积既可能是三个最大正数的乘积,也可能是两个最小负数(即绝对值最大)与最大正数的乘积。 综上,我们在给数组排序后,分别求出三个 阅读全文
posted @ 2021-04-14 15:50 kpwong 阅读(119) 评论(0) 推荐(0) 编辑
摘要: class Solution { public int findMin(int[] nums) { int left = 0, right = nums.length - 1; int min = Integer.MAX_VALUE; // 宗旨 每次转换下标的时候,都会记录更新一下min whil 阅读全文
posted @ 2021-04-14 15:43 kpwong 阅读(29) 评论(0) 推荐(0) 编辑
摘要: /* * 121. Best Time to Buy and Sell Stock * 题意:股票买卖1次,最大利润 * 难度:Easy * 分类:Arryas, Dynamic Programming * Tips:lc121, lc309, lc188, lc123, lc714 */ publ 阅读全文
posted @ 2021-04-14 15:35 kpwong 阅读(30) 评论(0) 推荐(0) 编辑
摘要: public static boolean isValidBST(TreeNode root) { if(root==null) return true; return dfs(root, -Double.MAX_VALUE, Double.MAX_VALUE); // 注意Double.MIN_V 阅读全文
posted @ 2021-04-14 15:30 kpwong 阅读(40) 评论(0) 推荐(0) 编辑
摘要: /* * 300. Longest Increasing Subsequence * 题意:最长递增子数组,不一定是连续的 * 难度:Medium * 分类:Binary Search, Dynamic Programming * 思路:基本的思路是dp[i]记录以nums[i]结尾的最长长度,每次 阅读全文
posted @ 2021-04-14 15:24 kpwong 阅读(41) 评论(0) 推荐(0) 编辑
摘要: class Solution { public int[][] generateMatrix(int n) { int maxNum = n * n; int curNum = 1; int[][] matrix = new int[n][n]; int row = 0, column = 0; i 阅读全文
posted @ 2021-04-14 15:20 kpwong 阅读(39) 评论(0) 推荐(0) 编辑
摘要: import java.util.ArrayList; import java.util.List; /* * 119. Pascal's Triangle II * 题意:和118一样,就是输出不一样 * 难度:Easy * 分类:Array * 思路:记一下 ArrayList.set 方法,不 阅读全文
posted @ 2021-04-14 15:14 kpwong 阅读(27) 评论(0) 推荐(0) 编辑
摘要: class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<Integer>(); inorder(root, res); return res; 阅读全文
posted @ 2021-04-14 15:11 kpwong 阅读(24) 评论(0) 推荐(0) 编辑
摘要: /* * 62. Unique Paths * 题意:求从数组[0,0]走到[m,n]的不同路径数 * 难度:Medium * 分类:Array, Dynamic Programming * 思路:和lc63, lc64思路一样, arr存储的内容由路径数换成了和 */ public class l 阅读全文
posted @ 2021-04-14 15:06 kpwong 阅读(24) 评论(0) 推荐(0) 编辑
摘要: package code; /* * 25. Reverse Nodes in k-Group * 题意:每k个反转一下,不足k的不反转,直接接上 * 难度:Hard * 分类:Linked List * 思路:递归调用反转,反转完下一段的返回节点,节点这一段上 * Tips:lc25, lc206 阅读全文
posted @ 2021-04-14 15:00 kpwong 阅读(31) 评论(0) 推荐(0) 编辑
摘要: // start our "pointer" in the bottom-left class Solution { public boolean searchMatrix(int[][] matrix, int target) { // start our "pointer" in the bot 阅读全文
posted @ 2021-04-14 14:56 kpwong 阅读(29) 评论(0) 推荐(0) 编辑
摘要: import java.util.ArrayDeque; import java.util.ArrayList; import java.util.List; import java.util.Queue; public class lc103 { public class TreeNode { i 阅读全文
posted @ 2021-04-14 14:37 kpwong 阅读(25) 评论(0) 推荐(0) 编辑
摘要: import java.util.LinkedList; import java.util.Queue; class HitCounter { private Queue<Integer> q = new LinkedList<Integer>(); /** Initialize your data 阅读全文
posted @ 2021-04-14 14:21 kpwong 阅读(73) 评论(0) 推荐(0) 编辑
摘要: hi /* * 152. Maximum Product Subarray * 题意:连续子序列最大乘积 * 难度:Medium * 分类:Array, Dynamic Programming * 思路:保存最大,最小值,因为负负得正。dp不用保存数组,空间可以压缩。 * Tips:Product是 阅读全文
posted @ 2021-04-14 11:29 kpwong 阅读(28) 评论(0) 推荐(0) 编辑