随笔分类 - Leecode
摘要:给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。 示例 1: 输入:head = [1,2,3,4,5], left = 2, right = 4 输出:[1,4
阅读全文
摘要:方法三:迭代二分查找 public int findPeakElement(int[] nums) { //第二种方法不用左右加个边界 return helper(nums, 0, nums.length-1); } public int helper(int[] nums, int left, i
阅读全文
摘要:/* * 15. 3Sum * 题意:找出数组中所有和为0的三元组合 * 难度:Medium * 分类:Array, Two Pointers * 注意:如何避免 List 重复元素 * Tips:lc15, lc16, lc923 */ import java.util.*; public cla
阅读全文
摘要:/* * 146. LRU Cache * 题意:首先理解LRU思想,最久未被访问过的,最先被替换 * 难度:Hard * 分类:Design * 思路:hashmap + 双向链表。hashmap实现了O(1)的get,双向链表实现O(1)的put * Tips:能想到双向链表,就不难了 * lc
阅读全文
摘要: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
阅读全文
摘要:package code; /* * 141. Linked List Cycle * 题意:链表是否有环 * 难度:Easy * 分类:Linked List, Two Pointers * 思路:快慢指针 * lc142 */ public class lc141 { public class
阅读全文
摘要:/* * 543. Diameter of Binary Tree * 题意:树中的最长路径 * 难度:Easy * 分类:Tree * 思路:和lc124思路一样,但lc124是Hard,这道竟然是Easy,哈哈哈 * Tips: */ public class lc543 { public cl
阅读全文
摘要:方法一:排序首先将数组排序。 如果数组中全是非负数,则排序后最大的三个数相乘即为最大乘积;如果全是非正数,则最大的三个数相乘同样也为最大乘积。 如果数组中有正数有负数,则最大乘积既可能是三个最大正数的乘积,也可能是两个最小负数(即绝对值最大)与最大正数的乘积。 综上,我们在给数组排序后,分别求出三个
阅读全文
摘要:class Solution { public int findMin(int[] nums) { int left = 0, right = nums.length - 1; int min = Integer.MAX_VALUE; // 宗旨 每次转换下标的时候,都会记录更新一下min whil
阅读全文
摘要:/* * 121. Best Time to Buy and Sell Stock * 题意:股票买卖1次,最大利润 * 难度:Easy * 分类:Arryas, Dynamic Programming * Tips:lc121, lc309, lc188, lc123, lc714 */ publ
阅读全文
摘要:public static boolean isValidBST(TreeNode root) { if(root==null) return true; return dfs(root, -Double.MAX_VALUE, Double.MAX_VALUE); // 注意Double.MIN_V
阅读全文
摘要:/* * 300. Longest Increasing Subsequence * 题意:最长递增子数组,不一定是连续的 * 难度:Medium * 分类:Binary Search, Dynamic Programming * 思路:基本的思路是dp[i]记录以nums[i]结尾的最长长度,每次
阅读全文
摘要: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
阅读全文
摘要:import java.util.ArrayList; import java.util.List; /* * 119. Pascal's Triangle II * 题意:和118一样,就是输出不一样 * 难度:Easy * 分类:Array * 思路:记一下 ArrayList.set 方法,不
阅读全文
摘要:class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<Integer>(); inorder(root, res); return res;
阅读全文
摘要:/* * 62. Unique Paths * 题意:求从数组[0,0]走到[m,n]的不同路径数 * 难度:Medium * 分类:Array, Dynamic Programming * 思路:和lc63, lc64思路一样, arr存储的内容由路径数换成了和 */ public class l
阅读全文
摘要:package code; /* * 25. Reverse Nodes in k-Group * 题意:每k个反转一下,不足k的不反转,直接接上 * 难度:Hard * 分类:Linked List * 思路:递归调用反转,反转完下一段的返回节点,节点这一段上 * Tips:lc25, lc206
阅读全文
摘要:// start our "pointer" in the bottom-left class Solution { public boolean searchMatrix(int[][] matrix, int target) { // start our "pointer" in the bot
阅读全文
摘要:import java.util.ArrayDeque; import java.util.ArrayList; import java.util.List; import java.util.Queue; public class lc103 { public class TreeNode { i
阅读全文
摘要:import java.util.LinkedList; import java.util.Queue; class HitCounter { private Queue<Integer> q = new LinkedList<Integer>(); /** Initialize your data
阅读全文
浙公网安备 33010602011771号