![](https://img-blog.csdnimg.cn/20190927151043371.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
leetcode
leetcode刷题笔记
ZyyIsPig
这个作者很懒,什么都没留下…
展开
-
Leetcode98
Leetcode98遗漏点改正代码遗漏点没有考虑周全,6应该小于15大于10代码:public class Solution { public boolean isValidBST(TreeNode root) { boolean ans = dfs(root, Integer.MAX_VALUE,'l'); return ans; } public boolean dfs(TreeNode root, int val, char dire原创 2020-11-24 17:48:58 · 86 阅读 · 0 评论 -
回溯Leetcode79
定义辅助变量mark标记访问过的元素,设置方向矩阵direct,辅助矩阵inMatrix判断当前的坐标是否越界,可以先采用简单的矩阵实验回溯回来要重置mark的当前位置为false//遍历如果当前的字母和字符串的字母相等,则向四个方向拓展 if(board[x][y] == word.charAt(count)){ mark[x][y] = true; for (int i = 0; i < 4; i++) {原创 2020-11-08 15:16:12 · 618 阅读 · 0 评论 -
详解Leetcode77深搜+剪枝
画图便于理解不带剪枝版for循环中dfs(n,k,i + 1,ans,curAns),i + 1标识下一个数从当前选择的数的下一个数开始选择,for循环的条件i<n不带剪枝条/** * 深度优先搜索 * @param n 搜索的空间 * @param k 子序列的长度 * @param idx 当前在搜索空间的位置 * @param ans 所有的答案序列 * @param curAns 当前的答案序列 */ priv原创 2020-11-04 10:19:49 · 163 阅读 · 0 评论 -
Leetcode74
转化为普通的二分查找,transform函数将长度转化为矩阵坐标例如长3宽3的矩阵长度为9,长度为4转化为矩阵坐标就是[2][0]/** * 将位置转为矩阵下标 * @param pos * @param columns * @return */ private int[] transform(int pos,int columns){ int row = pos / columns; int column =原创 2020-11-04 09:48:22 · 80 阅读 · 0 评论 -
Leetcode67
以长度较短的字符串为标准,使用StringBuilder方便添加字符,分情况讨论:a、b 两字符串长度相同讨论做完加法最后是否有进位a比b长讨论做完加法的进位和剩余未加的数字求和,求和完仍然考虑进位问题b比a长,和上一种情况类似代码如下public class Solution { public String addBinary(String a, String b) { char[] _a = a.toCharArray(); char原创 2020-11-02 23:41:33 · 110 阅读 · 0 评论 -
Leetcode63
动态规划不带障碍带障碍不带障碍分情况讨论,第一行、第一列以及其他情况其他情况中当前位置的值(即路径数)等于上方元素的值加左方元素的值/** * @author :hodor007 * @date :Created in 2020/10/31 * @description : * @version: 1.0 */public class Solution { public int uniquePaths(int m, int n) { int[][] matrix原创 2020-11-01 09:14:20 · 71 阅读 · 0 评论 -
Leetcode39
使用递归算法,将数组排序方便剪枝1.正序/** * 正序,注意题目条件,没有重复数字 */public class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { //排序成有序数组方便剪枝 Arrays.sort(candidates); List<List<Integer>&g原创 2020-10-28 23:40:59 · 120 阅读 · 0 评论 -
Leetcode116
题解BFS利用已经连接的next指针,逐层添加BFS很容易联想到广度优先遍历,逐层添加package Tree.Leetcode116.Solution1;import java.util.LinkedList;/** * 利用广度优先遍历,使用的也是常数级空间? */public class Solution { public Node connect(Node root) { if(root == null) return root;原创 2020-10-15 16:29:56 · 112 阅读 · 0 评论 -
Leetcode22
回溯DFSBFSDFS参数的subAns每次都使用新的字符串,回溯时无需修改当右括号的剩余数量大于左括号剩余数量时肯定时错误答案,这个分支可以去除import java.util.ArrayList;import java.util.List;/** * 使用另一种终结条件,依旧是dfs */public class Solution { public List<String> generateParenthesis(int n) { List<原创 2020-10-13 20:09:37 · 82 阅读 · 0 评论 -
Leetcode29
使用二分法?循环每次减去2n个divisor,count加上2n个divisor,直到dividend < divisorpackage Math.Leetcode29;public class Solution { public int divide(int dividend, int divisor) { if(divisor == 1) return dividend; if(divisor == -1){原创 2020-10-12 19:35:03 · 71 阅读 · 0 评论 -
Leetcode9
Leetcode9回文数转成字符串求解不转成字符串求解(还是使用了额外空间)不转成字符串求解(使用常数空间)转成字符串求解转为字符串,双指针判断求解public class Solution { public boolean isPalindrome(int x) { String s = String.valueOf(x); int l = 0; int r = s.length() - 1; while(l < r){原创 2020-10-10 15:20:51 · 63 阅读 · 0 评论 -
Leetcode16三数之和
思路与Leetcode15类似先进行排序,外循环遍历数组,内循环双指针寻找最接近的答案import java.util.Arrays;/** * 类似Leetcode15三数之和 */public class Solution { public int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); int len = nums.length; int ans原创 2020-10-09 14:34:25 · 52 阅读 · 0 评论 -
Leetcode92
画图有助于理解使用两个辅助节点接在头部,pre存储交换的链表段的前一个节点以便最后连接,q最后指向了链表段的后一个节点public class Solution { public ListNode reverseBetween(ListNode head, int m, int n) { //特判 if(head == null) return head; //添加再前面的两个辅助节点 ListNode原创 2020-10-06 10:38:35 · 62 阅读 · 0 评论 -
Leetcode18
思路与Leetcode15三数之和类似,外两层循环遍历两个数,再使用左右指针找另外两个数数组必须先进行排序import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { List<List<原创 2020-10-06 00:11:04 · 63 阅读 · 0 评论 -
Leecode15
先进行排序i进行遍历数组,l= i + 1,r = i - 1while(l < r)nums[i] + nums[l] + nums[r] < 0是nums[l]小了nums[i] + nums[l] + nums[r] > 0是nums[r]大了import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class Solution { //去重是关键原创 2020-10-05 20:48:11 · 83 阅读 · 0 评论 -
Leetcode91
对字符串2223进行分析,从字符串末尾向前递推首字母为零、组合起来大于26的情况为0种对230分析public class Solution { public int numDecodings(String s) { if(s == null || s.length() == 0) return 0; int len = s.length(); int[] arr = new int[len]; /原创 2020-10-03 23:57:18 · 63 阅读 · 0 评论 -
字符串leecode49
调用API解决,建立HashMap,以排序后的字符串为键,排序后相同的字符串组成的List为值import java.util.*;public class Solution { public List<List<String>> groupAnagrams(String[] strs) { List<List<String>> ans = new ArrayList<>(); //特判原创 2020-09-29 18:57:42 · 69 阅读 · 0 评论 -
Leecode41
【打卡】原数组作为哈希表,下标为键,对应数组位置的值为值,如果i位置的值小于0,则表示i存在总共进行三轮遍历数组nums第一轮判断1是否存在,不存在则1就是答案,存在则1可以用作辅助(在第二轮中把小于等于0的数改成1)[2,1,-4,5,6,3,0][2,1,1,5,6,3,1]第二轮遍历数组,将|nums[i]|-1的位置的数值改为负数,表示|nums[i]|这个数出现过,如果|nums[i]|-1超过数组长度就将第一个位置改为负数[2,-1,1,5,6,3,1][-2,-1,1,5,-6,原创 2020-09-28 19:43:27 · 77 阅读 · 0 评论 -
排序Leetcode179
通过重写comparator使用Arrays.sort解决comparator中根据比较不同的拼接方式生成的字符串的大小返回比较的结果import java.util.Arrays;import java.util.Comparator;public class Solution { public String largestNumber(int[] nums) { //思路:高位大的数放前面 //问题:如何设计比较的函数,数字的位数不同如何比较,使用has原创 2020-09-28 10:40:15 · 70 阅读 · 0 评论 -
Leetcode11
水题打卡暴力遍历双指针暴力遍历两层循环遍历public class Solution { public int maxArea(int[] height) { int len = height.length; int max_capacity = 0; for (int i = 0; i < len; i++) { for (int j = i + 1; j < len; j++) {原创 2020-09-27 16:47:53 · 108 阅读 · 0 评论 -
链表翻转Leetcode25
先定义整体的思路,使用四个指针迭代,不要怂,推导第二次循环再修改完善条件思路是官方的题解:官方题解public class Solution { //不要怂,推导第二次循环再修改完善条件 //先定义整体的思路,使用四个指针迭代 public ListNode reverseKGroup(ListNode head, int k) { //特判 if(head == null || k == 1) return head;原创 2020-09-27 10:58:27 · 50 阅读 · 0 评论 -
N皇后
Leetcode51回溯解决,难点在于辅助变量的定义,发现判断是否是同一条斜线的规律,详见注释辅助变量//定义辅助变量 Set<Integer> columns = new HashSet<>();//行检测 Set<Integer> diagonal1 = new HashSet<>();//对角线检测,同一条对角线当前行左边减去列坐标或者加上列坐标是相等的 Set<Integer> diag原创 2020-09-26 08:29:48 · 51 阅读 · 0 评论 -
Leetcode5(动态规划)
状态转移方程flag[i][j] = flag[i + 1][j - 1] && s.charAt(i) == s.charAt(j)flag[i][j] = true表示从下标i到j的字串是回文串,下标i + 1到j - 1的字串是否是回文串已经判断过了,如果为true且首尾字符相同则这个字串是回文串动态规划public class Solution { public String longestPalindrome(String s) { int len原创 2020-09-24 20:56:10 · 418 阅读 · 0 评论 -
二分法Leetcode69
水题打卡使用的二分法有些情况定位不到答案如求8的平方根,使用额外的变量存储解决问题数字特别大的情况卡用例,将类型转为long解决public class Solution { public static void main(String[] args) { Solution solution = new Solution(); int ans = solution.mySqrt(226); System.out.println(ans);原创 2020-09-24 11:04:01 · 94 阅读 · 0 评论 -
贪心算法Leetcode55
思路类似Leetcode45,每步都走最远的距离,如果最远能够到达的位置不再发生变化则说明已经停下来了,到达不了最后的节点(这种情况一定是遇到0了,但是遇到0只要能跳过就能到达最后的节点)详细见注释public class Solution { public boolean canJump(int[] nums) { //只有一个节点显然是可以到达的 if(nums.length == 1) return true; in原创 2020-09-23 20:54:22 · 75 阅读 · 0 评论 -
回溯Leecode37
写代码没有思路,先将辅助的变量定义好和初始化有助于厘清思路难点在于一个空格所有数字都尝试失败之后如何回到上一个状态,可以使用List存储需要填写的空格方便回溯使用还有一个要注意的点是每一格空格填数字的循环中要加上!valid条件,否则得到正确答案后回到上一个空格的遍历循环时还是会继续执行//每个空格执行进行所有数字的尝试for (int i = 0; i < 9 && !valid; i++){}完整代码(带测试用例):import java.util.ArrayLis原创 2020-09-23 09:19:45 · 531 阅读 · 0 评论 -
Leetcode36
Leetcode36通过本题学会了API的使用(,使用HashMap标志是否存在于行、列或者小区域中,暴力遍历public boolean isValidSudoku(char[][] board) { HashMap<Integer,Integer>[] rows = new HashMap[9]; HashMap<Integer,Integer>[] columns = new HashMap[9]; HashMap<In原创 2020-09-22 20:16:41 · 49 阅读 · 0 评论 -
树的三种遍历方法(迭代算法)
树的三种遍历方式中序遍历前序遍历后序遍历中序遍历Leecode94,当根节点遍历完成的时候,需要根节点进行中转到右子树import java.util.ArrayDeque;import java.util.ArrayList;import java.util.List;public class Solution { public static void main(String[] args) { TreeNode t1 = new TreeNode(1);原创 2020-09-22 15:29:59 · 1207 阅读 · 0 评论 -
Leecode135
Leecode135使用双数组,分别对左右做局部最优,取较大值import java.util.Arrays;/*使用贪心算法:每个数左右两边各自局部最优,取两者的较大值,获得的就是全局最优 */public class Solution2 { public static void main(String[] args) { int[] rating = {1,2,2}; int candy = new Solution2().candy(rating原创 2020-09-15 08:24:35 · 52 阅读 · 0 评论 -
Leecode338
动态规划解法import java.util.Arrays;//动态规划,找出递推的规律public class Solution2 { public static void main(String[] args) { int[] ans = new Solution2().countBits(5); System.out.println(Arrays.toString(ans)); } public int[] countBits(int原创 2020-09-13 08:15:20 · 79 阅读 · 0 评论 -
Leecode128
Leecode128使用hashset解决空间复杂度O(n),额外使用了hashset存储事件复杂度O(n),内层循环中数组里每个数仅仅遍历了一次import java.util.HashSet;public class Solution2 { public static void main(String[] args) { int[] nums = {100, 4, 200, 1, 3, 2}; int i = new Solution2().longe原创 2020-09-11 19:39:26 · 63 阅读 · 0 评论 -
Leecode71
Leecode71path在split之后的数组中有空字符串,大坑 import java.util.Stack;//存在空串public class Solution { public static void main(String[] args) {// Stack<String> stack = new Stack<>();// String str = "/a/./b///../c/../././../d/..//../e/.原创 2020-09-08 21:28:10 · 52 阅读 · 0 评论 -
2020-09-03
Leecode189简单反转,面向测试用例编程(public class Solution { public static void main(String[] args) { Solution s = new Solution(); int[] arr = {1,2}; s.rotate(arr,0); } public void rotate(int[] nums, int k) { //排除特例原创 2020-09-03 20:28:27 · 72 阅读 · 0 评论 -
Leecode23
Leecode23使用优先级队列和分治法解决class Solution { public static void main(String[] args) { Solution s = new Solution(); ListNode l11 = new ListNode(1); ListNode l12 = new ListNode(4); ListNode l13 = new ListNode(5); l11.n原创 2020-08-31 16:49:00 · 79 阅读 · 0 评论 -
剑指Offer32
剑指Offer32BFS遍历public class Solution { static List<List<Integer>> res = new ArrayList<>(); public static void main(String[] args) { } public List<List<Integer>> levelOrder(TreeNode root) { LinkedLis原创 2020-08-31 13:16:50 · 63 阅读 · 0 评论 -
2020-08-27
Leecode144使用递归和栈两种方法public class Solution { public static void main(String[] args) { TreeNode t1 = new TreeNode(1); TreeNode t2 = new TreeNode(2); TreeNode t3 = new TreeNode(3); t1.left = null; t1.right = t2;原创 2020-08-27 07:28:40 · 56 阅读 · 0 评论 -
2020-08-26
Leecode61计算出需要移动的链表的长度移到链表首public class Solution { public static void main(String[] args) { ListNode l1 = new ListNode(1); ListNode l2 = new ListNode(2); ListNode l3 = new ListNode(3); ListNode l4 = new ListNode(4);原创 2020-08-26 21:36:42 · 57 阅读 · 0 评论 -
2020-08-26
Leecode445分别使用栈和链表反转两种方法/*输入:(7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 8 -> 0 -> 7 */public class Solution { public static void main(String[] args) { ListNode l1 = new ListNode(7); ListNode l2 = new ListNod原创 2020-08-26 16:43:47 · 88 阅读 · 0 评论 -
2020-08-25
Leecode102BFSpublic class Solution { public static void main(String[] args) { TreeNode t1 = new TreeNode(3); TreeNode t2 = new TreeNode(9); TreeNode t3 = new TreeNode(20); TreeNode t4 = new TreeNode(15); TreeNo原创 2020-08-25 08:18:48 · 117 阅读 · 0 评论 -
2020-08-23
使用三指针操作,h指针方便删除节点原创 2020-08-23 14:49:05 · 74 阅读 · 0 评论