LeetCode
刷题笔记
玳宸
这个作者很懒,什么都没留下…
展开
-
【LeetCode】45. Jump Game II(中等难度)
贪心法方法一 正向查找可到达的最大位置如果我们「贪心」地进行正向查找,每次找到可到达的最远位置,就可以在线性时间内得到最少的跳跃次数。例如,对于数组 [2,3,1,2,4,2,3],初始位置是下标 0,从下标 0 出发,最远可到达下标 2。下标 0 可到达的位置中,下标 1 的值是 3,从下标 1 出发可以达到更远的位置,因此第一步到达下标 1。从下标 1 出发,最远可到达下标 4。下标 1 可到达的位置中,下标 4 的值是 4 ,从下标 4 出发可以达到更远的位置,因此第二步到达下标 4。在.原创 2021-10-22 11:30:44 · 206 阅读 · 0 评论 -
【LeetCode】5. Longest Palindromic Substring(中等难度)
方法一 动态规划思路: 对于一个字串而言,如果它是回文串,并且长度大于 2,那么将它首尾的两个字母去除之后,它仍然是个回文串。根据这样的思路,我们就可以用动态规划的方法解决本题。我们用 P(i,j) 表示字符串 s 的第 i 到 j 个字母组成的串(下文表示成 s[i:j])是否为回文串:这里的「其它情况」包含两种可能性:s[i,j] 本身不是一个回文串;i > j,此时 s[i,j] 本身不合法。写出动态规划的状态转移方程:写出动态规划的边界条件:根据这个思路,我们就可以.原创 2021-10-22 10:43:23 · 97 阅读 · 0 评论 -
【LeetCode】64. Minimum Path Sum(中等难度)
方法一直接用递推公式class Solution { public int minPathSum(int[][] grid) { int m = grid.length; int n = grid[0].length; //由于第一行和第一列不能用递推公式,所以单独更新 for(int i = 1; i < n; i++){ grid[0][i] = grid[0][i - 1] + grid[0][.原创 2021-10-21 21:33:52 · 70 阅读 · 0 评论 -
【LeetCode】63. Unique Paths II(中等难度)
方法一 动态规划由于 dp[i][j] = dp[i-1][j]+dp[i][j-1] 等于 dp[j]+=dp[j-1],其中 dp[j] 就相当于 dp[i-1][j],dp[i][j-1] 就相当于 dp[j-1],就从二维压缩到一维了。class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { int n = obstacleGrid.length, m = obstacleG.原创 2021-10-21 21:24:11 · 73 阅读 · 0 评论 -
【LeetCode】59. Spiral Matrix II(中等难度)
和 54 题简直一模一样也是一圈一圈遍历,然后边遍历边从 1 ~ n^2 存值class Solution { public int[][] generateMatrix(int n) { int num = 1; int[][] res = new int[n][n]; int left = 0, right = n - 1, top = 0, bottom = n - 1; while(left <= right &.原创 2021-10-21 20:50:22 · 66 阅读 · 0 评论 -
【LeetCode】57. Insert Interval(中等难度)
在给定的区间集合已经按照左端点排序的前提下,所有与区间 S 重叠的区间在数组 intervals 中下标范围是连续的,因此我们可以对所有的区间进行一次遍历,就可以找到这个连续的下标范围。class Solution { public int[][] insert(int[][] intervals, int[] newInterval) { int left = newInterval[0]; int right = newInterval[1]; ..原创 2021-10-21 20:23:51 · 100 阅读 · 0 评论 -
【LeetCode】56. Merge Intervals(中等难度)
方法一 排序如果我们按照区间的左端点排序,那么在排完序的列表中,可以合并的区间一定是连续的。如下图所示,标记为蓝色、黄色和绿色的区间分别可以合并成一个大区间,它们在排完序的列表中是连续的:用数组 res 存储最终的答案。首先,我们将列表中的区间按照左端点升序排序。然后我们将第一个区间加入 res 数组中,并按顺序依次考虑之后的每个区间:如果当前区间的左端点在数组 res 中最后一个区间的右端点之后,那么它们不会重合,我们可以直接将这个区间加入数组 res 的末尾;否则,它们重合,我们需要用当.原创 2021-10-20 14:02:31 · 117 阅读 · 0 评论 -
【LeetCode】55. Jump Game(中等难度)
方法一 贪心对于当前遍历到的位置 x,如果它在最远可以到达的位置的范围内,那么我们就可以从起点通过若干次跳跃到达该位置,因此我们可以用 x+nums[x] 更新最远可以到达的位置。在遍历的过程中,如果最远可以到达的位置大于等于数组中的最后一个位置,那就说明最后一个位置可达,我们就可以直接返回 True 作为答案。反之,如果在遍历结束后,最后一个位置仍然不可达,我们就返回 False 作为答案。class Solution { public boolean canJump(int[] nums.原创 2021-10-20 12:52:44 · 93 阅读 · 0 评论 -
【LeetCode】54. Spiral Matrix(中等难度)
方法一 按层输出可以将矩阵看成若干层,首先输出最外层的元素,其次输出次外层的元素,直到输出最内层的元素。对于每层,从左上方开始以顺时针的顺序遍历所有元素。假设当前层的左上角位于 (top,left),右下角位于 (bottom,right),按照如下顺序遍历当前层的元素。从左到右遍历上侧元素,依次为 (top,left) 到 (top,right)。从上到下遍历右侧元素,依次为 (top+1,right) 到 (bottom,right)。如果 left<right 且 top<b.原创 2021-10-20 11:07:23 · 114 阅读 · 0 评论 -
【LeetCode】48. Rotate Image(中等难度)
方法一⼀圈⼀圈的循环交换class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for(int i = 0; i < n/2; i++){ for(int j =i; j < n-i-1; j++){ int temp = matrix[i][j]; matrix[.原创 2021-10-15 12:44:03 · 66 阅读 · 0 评论 -
【LeetCode】47. Permutations II(中等难度)
和 46 题非常非常非常类似,主要就是去重的问题方法一 回溯法之前没有重复的元素,所以可以直接在 temp 判断有没有当前元素,有的话就跳过。但这⾥的话,因为给定的有重复的元素,这个⽅法明显不可以了。换个思路,我们可以再⽤⼀个 list 保存当前 temp 中已经有的元素的下标,然后添加新元素的时候去判断下标就可以了。第⼆个问题就是,可以看到有重复元素的时候,上边第 1 个图和第 2 个图产⽣的是完全⼀样的序列。所以第 2 个遍历是没有必要的。解决的⽅案就是把数组⾸先排下顺序,然后判断⼀下上⼀.原创 2021-10-15 11:08:44 · 73 阅读 · 0 评论 -
【LeetCode】46. Permutations()中等难度
方法一 回溯法啊 一看完题目就想到回溯,利⽤递归每次向 temp ⾥添加⼀个数字,数字添加够以后再回来进⾏回溯,再向后添加新的解。可以理解成⼀层⼀层的添加,每⼀层都是⼀个 for 循环每调⽤⼀层就进⼊⼀个 for 循环,相当于列出了所有解,然后挑选了我们需要的。其实本质上就是深度优先遍历 DFS。class Solution { public List<List<Integer>> permute(int[] nums) { List<Lis.原创 2021-10-15 10:25:31 · 68 阅读 · 0 评论 -
【LeetCode】40. Combination Sum II(中等难度)
基本思想与 39 类似,也是回溯法,主要就是去重了class Solution { private List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<Integer> combine = new Arra.原创 2021-10-14 13:03:48 · 58 阅读 · 0 评论 -
【LeetCode】39. Combination Sum(中等难度)
方法一 回溯法(未剪枝)就是向前列举所有情况,每个值都有直接跳过和添加两个选择,得到一个解或者走不通的时候就回溯。class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<>(); List<In.原创 2021-10-14 11:22:38 · 80 阅读 · 0 评论 -
【LeetCode】36. Valid Sudoku(中等难度)
用三个表存数据class Solution { public boolean isValidSudoku(char[][] board) { int[][] rows = new int[9][9]; int[][] columns = new int[9][9]; int[][][] subboxes = new int[3][3][9]; for (int i = 0; i < 9; i++) { .原创 2021-10-13 14:08:42 · 63 阅读 · 0 评论 -
【LeetCode】34. Find First and Last Position of Element in Sorted Array(中等难度)
解法一 二分法直观的思路肯定是从前往后遍历一遍。用两个变量记录第一次和最后一次遇见的下标,但这个方法的时间复杂度为 O(n),没有利用到数组升序排列的条件。由于数组已经排序,因此整个数组是单调递增的,我们可以利用二分法来加速查找的过程。考虑开始和结束位置,其实我们要找的就是数组中「第一个等于 target 的位置」(记为 l)和「第一个大于 target 的位置减一」(记为 r)。二分查找中,寻找 l 即为在数组中寻找第一个大于等于 target 的下标,寻找 r 即为在数组中寻找第一个大于 ta.原创 2021-10-13 11:23:47 · 89 阅读 · 0 评论 -
【LeetCode】33. Search in Rotated Sorted Array(中等难度)
解法一emm 直接遍历比较class Solution { public int search(int[] nums, int target) { for(int i = 0; i < nums.length; i++){ if(nums[i] == target){ return i; } } return -1; }}解法二 二分查找将数组.原创 2021-10-13 10:36:08 · 50 阅读 · 0 评论 -
【LeetCode】31. Next Permutation(中等难度)
将该问题形式化地描述为:给定若干个数字,将其组合为一个整数。如何将这些数字重新排列,以得到下一个更大的整数。如 123 下一个更大的数为 132。如果没有更大的整数,则输出最小的整数。以 1,2,3,4,5,6 为例,其排列依次为:123456123465123546…654321可以看到有这样的关系:123456 < 123465 < 123546 < … < 654321。算法推导如何得到这样的排列顺序?我们可以这样来分析:我们希望下一个数比当前数大,这.原创 2021-10-11 21:43:51 · 150 阅读 · 0 评论 -
【LeetCode】18. 4Sum(中等难度)
和15题非常非常类似参照第15题的解法。class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { Arrays.sort(nums); List<List<Integer>> res = new LinkedList<>(); for(int i = 0; i < nums.len.原创 2021-10-11 15:05:56 · 63 阅读 · 0 评论 -
【LeetCode】16. 3Sum Closest(中等难度)
和15题非常类似解法和上一题一样class Solution { public int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); int sub = Integer.MAX_VALUE; int sum = 0; for(int i=0; i<nums.length-2; i++){ int l=i+1, h=nums.原创 2021-10-11 14:41:34 · 114 阅读 · 0 评论 -
【LeetCode】15. 3Sum(中等难度)
解法一 暴力三层循环,遍历所有的情况。但需要注意的是,我们需要把重复的情况去除掉,就是 [1, -1 ,0] 和 [0, -1, 1] 是属于同⼀种情况的。class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> res = new ArrayList<List<Integer>>(); .原创 2021-10-11 13:29:24 · 169 阅读 · 0 评论 -
【LeetCode】12. Integer to Roman(中等难度)
解法一主要思想就是每次取出⼀位,然后得到相应的罗⻢数字,然后合起来就⾏。class Solution { public String getRoman(int num, int count){ { //count 表示当前的位数,个位,⼗位... char[] ten = {'I', 'X', 'C', 'M'}; char[] five = {'V', 'L', 'D'}; String r = ""; if(num <..原创 2021-10-11 10:50:09 · 70 阅读 · 0 评论 -
【LeetCode】11. Container With Most Water(中等难度)
每个数组代表⼀个⾼度,选两个任意的柱⼦往⾥边倒⽔,能最多倒多少⽔。解法一 暴力解法直接遍历任意两根柱⼦,求出能存⽔的⼤⼩,⽤⼀个变量保存最⼤的。class Solution { public int maxArea(int[] height) { int max = 0; for(int i = 0;i < height.length; i++){ for(int j = i + 1; j < height.length; .原创 2021-10-10 21:39:15 · 51 阅读 · 0 评论 -
【LeetCode】69. Sqrt(x)(简单难度)
求一个数的平方根,不要求近似解,只需要整数部分。解法一 二分法求 n 的平方根的整数部分,所以平方根一定是 1,2,3 … n 中的一个数。从一个有序序列中找一个数,像极了二分查找。先取中点 mid,然后判断 mid * mid 是否等于n,小于 n 的话取左半部分,大于 n 的话取右半部分,等于 n 的话 mid 就是我们要找的了。class Solution { public int mySqrt(int x) { int L = 1, R = x; wh.原创 2021-07-05 21:28:28 · 163 阅读 · 1 评论 -
【LeetCode】67. Add Binary(简单难度)
解法一直接按位相加class Solution { public String addBinary(String a, String b) { StringBuilder ans = new StringBuilder(); int i = a.length() - 1; int j = b.length() - 1; int carry = 0; while(i >= 0 || j >= 0){ .原创 2021-07-05 11:06:12 · 74 阅读 · 0 评论 -
【LeetCode】66. Plus One(简单难度)
解法一 递归class Solution { private int[] plusOneAtIndex(int[] digits, int index){ //说明每一位都是9 if(index < 0){ //新建一个更大的数组,最高位赋值为1 int[] ans = new int[digits.length + 1]; ans[0] = 1; return..原创 2021-07-03 14:08:47 · 108 阅读 · 0 评论 -
【LeetCode】58. Length of Last Word(简单难度)
直接从后往前遍历,遇到空格就停止。class Solution { public int lengthOfLastWord(String s) { int count = 0; int index = s.length() - 1; //过滤空格 while(true){ if(index < 0 || s.charAt(index) != ' '){ break; .原创 2021-07-03 13:44:45 · 49 阅读 · 0 评论 -
【LeetCode】53. Maximum Subarray(简单难度)
解法一 动态规划思路一用一个二维数组 dp[ i ][ len ] 表示从下标i开始,长度为 len 的子数组的元素和。这样长度是 len+1 的子数组就可以通过长度是 len 的子数组去求,也就是下边的递推式,dp [ i ] [ len + 1 ] = dp[ i ] [ len ] + nums [ i + len - 1]。考虑到求 i+1的情况的时候,我们只需要 i 时候的情况,所有我们其实没必要用一个二维数组,直接用一维数组就可以了。class Solution { pub..原创 2021-07-01 21:47:31 · 159 阅读 · 2 评论 -
【LeetCode】35. Search Insert Position(简单难度)
采用二分查找class Solution { public int searchInsert(int[] nums, int target) { int start = 0; int end = nums.length - 1; if(nums.length == 0) return 0; while(start < end){ int mid = (start + end)/2; ..原创 2021-06-30 21:02:45 · 52 阅读 · 0 评论 -
【LeetCode】28. Implement strStr()(简单难度)
直接一一比较class Solution { public int strStr(String haystack, String needle) { if(needle.length() == 0) return 0; int j = 0; for(int i = 0; i < haystack.length(); i++){ if(haystack.charAt(i) == needle.charAt(j)){...原创 2021-06-30 17:59:33 · 60 阅读 · 0 评论 -
【LeetCode】27. Remove Element(简单难度)
和【LeetCode】26. Remove Duplicates from Sorted Array(简单难度)类似,只不过这个是去除给定的值,看起来还更简单些。例如给了 nums = [3, 2, 2, 3],val = 3,然后我们返回 len=2,并且 nums 修改为 [2, 2]。解法一和上道题一样,我们利用快慢指针,此外我们还得用下反向的思维。快指针 fast 和慢指针 slow,一直移动 fast,如果 fast 指向的值不等于给定的 val,我们就将值赋给 slow 指向的位置,...原创 2021-06-29 11:35:19 · 130 阅读 · 0 评论 -
【LeetCode】26. Remove Duplicates from Sorted Array(简单难度)
返回非重复数字的个数,并且把 nums 里重复的数字也去掉。例如,nums = [1, 1, 2],那么就返回 2,并且把 nums 变成 [1, 2]。利用快慢指针,i 指针从 0 开始,j 指针从 1 开始,如果 i 和 j 所指数字相等,就一直后移 j。如果不相等,i 指针后移—位用来保存当前 j 所指的值,然后继续回到 j 的后移中去。class Solution { public int removeDuplicates(int[] nums) { if(num...原创 2021-06-28 21:04:04 · 191 阅读 · 0 评论 -
【LeetCode】21. Merge Two Sorted Lists(简单难度)
解法一 迭代/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = nex...原创 2021-06-27 21:59:20 · 60 阅读 · 0 评论 -
【LeetCode】20. Valid Parentheses(简单难度)
如果只有一种括号,我们完全可以用一个计数器 count,遍历整个字符串,遇到左括号加 1,遇到右括号减 1,遍历结束后,如果 count 等于0,则表示全部匹配。但如果有多种括号,像( [ ) ] 这种情况它依旧会得到 0,所以我们需要用其他的方法。栈!遍历整个字符串,遇到左括号就入栈,然后遇到和栈顶对应的右括号就出栈,遍历结束后,如果栈为空,就表示全部匹配。class Solution { public boolean isValid(String s) { Stac...原创 2021-06-26 16:10:56 · 57 阅读 · 0 评论 -
【LeetCode】14. Longest Common Prefix(简单难度)
解法一 垂直比较我们把所有字符串垂直排列,然后一列一列的比较,直到某一个字符串到达结尾或者该列字符不完全相同。class Solution { //判断 index 列的字符是否完全相同 public boolean isSameAtIndex(String[] strs, int index){ int i = 0; while(i < strs.length - 1){ if(strs[i].charAt(index...原创 2021-06-25 23:15:24 · 157 阅读 · 0 评论 -
【LeetCode】13. Roman to Integer(简单难度)
解法一先来一种不优雅的,也就是遍历字符串,然后转换就可以,但同时得考虑 Ⅳ,X 那些特殊情况。class Solution { public int getInt(char r){ int ans = 0; switch(r){ case 'I': ans = 1; break; case 'V': ans = 5...原创 2021-06-24 21:16:26 · 152 阅读 · 1 评论 -
【LeetCode】9. Palindrome Number(简单难度)
判断是不是回文数,负数不是回文数。解法一把 int 转成字符串,然后判断是否是回文串做就可以了,缺点是需要额外的空间存储字符串,当然题目也告诉了不能这样,所以 pass 。解法二在第 7 道题写了倒置 int 的算法,这里当然可以用到了,只需要判断倒置前后相不相等就可以了。当倒置后的数字超出 int 的范围时,我们返回的是 0,那么它一定不等于原数,此时一定返回 false 了,这正不正确呢?我们只需证明,如果倒置后超出 int的范围,那么它一定不是回文数字就好了。反证法,我们假设存在这...原创 2021-06-23 21:54:32 · 100 阅读 · 2 评论 -
【LeetCode】7.Reverse Interger(简单难度)
第一反应就是,取余得到个位数,然后除以 10 去掉个位数,然后用一个变量保存倒置的数。class Solution { public int reverse(int x) { int rev = 0; while(x != 0){ int pop = x % 10; x /= 10; rev = rev * 10 + pop; } return rev; ..原创 2021-06-22 20:33:59 · 93 阅读 · 1 评论 -
【LeetCode】1. Two Sum(简单难度)
解法一两重循环,遍历所有情况看相加是否等于目标和,如果符合直接输出。class Solution { public int[] twoSum(int[] nums, int target) { int[] ans = new int[2]; for(int i=0;i<nums.length;i++){ for(int j=i+1;j<nums.length;j++){ if(nums[i]...原创 2021-06-21 20:54:06 · 159 阅读 · 2 评论