随笔分类 -  leetCode

摘要:双指针 class Solution { public int minSubArrayLen(int s, int[] nums) { if (nums.length == 0) return 0; int ans = Integer.MAX_VALUE; int start = 0, end = 阅读全文
posted @ 2020-10-22 11:19 消灭猕猴桃 阅读(98) 评论(0) 推荐(0)
摘要:递归 class Solution { // 1. 确定函数参数以及返回值 public TreeNode trimBST(TreeNode root, int low, int high) { // 2.确定终止条件 if (root == null) return null; // 3. 确定单 阅读全文
posted @ 2020-10-22 09:36 消灭猕猴桃 阅读(115) 评论(0) 推荐(0)
摘要:贪心算法+双指针 class Solution { public List<Integer> partitionLabels(String S) { // 字符串S中每个字母最后出现的下标 int[] last = new int[26]; int length = S.length(); for 阅读全文
posted @ 2020-10-22 08:38 消灭猕猴桃 阅读(63) 评论(0) 推荐(0)
摘要:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ 阅读全文
posted @ 2020-10-21 11:30 消灭猕猴桃 阅读(71) 评论(0) 推荐(0)
摘要:双指针 typed中的字符有两种情况: name中的一部分,匹配那么中的一个字符长按键入的一部分,它应与前一个键入的字符相同 如果typed中的字符两个条件均未满足则返回false,否则当typed扫描完毕后检查name中每个字符是否匹配完毕。 使用两个下标 i, j追踪name,typed nam 阅读全文
posted @ 2020-10-21 10:35 消灭猕猴桃 阅读(107) 评论(0) 推荐(0)
摘要:回溯法 一共4个数字4种运算,可以通过回溯法遍历不同的可能性。 从4个数字中取出两个数字,并选择一种运算有 4*3=12种,将运算结果取代取出的两个数字等到3个数字在剩下三个数字种取出两个数字并选择一种运算有 3*2 = 6种, 将运算结果取代取出的两个数字还剩2数字剩下两个数字有两种不同的顺序,可 阅读全文
posted @ 2020-10-20 10:41 消灭猕猴桃 阅读(134) 评论(0) 推荐(0)
摘要:线性表 链表不能直接对下标进行访问,所以可以将链表的节点全部存入线性表中,然后就可以通过下标直接访问链表节点然后重建链表 class Solution { public void reorderList(ListNode head) { if (head == null) return; List< 阅读全文
posted @ 2020-10-20 09:17 消灭猕猴桃 阅读(85) 评论(0) 推荐(0)
摘要:dfs 遍历二维网格,如果网格值为“1”则以该网格为起点进行深度优先遍历,在搜索过程中值为1的网格都置为“0”。这样深度优先搜索的次数就是岛屿的数量。 class Solution { public int numIslands(char[][] grid) { int num = 0; int r 阅读全文
posted @ 2020-10-19 18:08 消灭猕猴桃 阅读(119) 评论(0) 推荐(0)
摘要:双指针 使用一个快指针与一个慢指针,快指针先移动n步,两个两个指针同时移动。当快指针到达最后一个节点后慢指针到达要删除节点的前一个节点,然后就可以将倒数第n个节点删除。如果快指针移动n步后为空,那么直接删除头节点 class Solution { public ListNode removeNthF 阅读全文
posted @ 2020-10-18 15:53 消灭猕猴桃 阅读(57) 评论(0) 推荐(0)
摘要:BFS 将所有的0构建为一个超级0,用超级0到终点的最短路径等价于多个源点到终点的最短路径 class Solution { // 定义一个方向数组 static int[][] dirs = new int[][]{{1,0}, {0,1}, {-1, 0}, {0, -1}}; public i 阅读全文
posted @ 2020-10-17 16:17 消灭猕猴桃 阅读(69) 评论(0) 推荐(0)
摘要:class Solution { // 代表一个搜索状态 class Nagoto { int x; // (x,y)为当前网格的坐标 int y; int rest; // 剩余能消除障碍的数量 Nagoto(int x, int y, int rest) { this.x = x; this.y 阅读全文
posted @ 2020-10-16 12:32 消灭猕猴桃 阅读(176) 评论(0) 推荐(0)
摘要:解法 双指针 定义两个指针指向数组前后,比较指向元素的平方大小,从答案数组之后向前填充 class Solution { public int[] sortedSquares(int[] A) { if (A == null) return null; int n = A.length; int[] 阅读全文
posted @ 2020-10-16 09:46 消灭猕猴桃 阅读(61) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { // 定义一个方向数组 static int[][] directions = new int[][]{{0, 1}, {1, 0}, {-1, 0},{0, -1}}; // X,Y的上限 static int limit = (int)1e6; p 阅读全文
posted @ 2020-10-15 10:54 消灭猕猴桃 阅读(120) 评论(0) 推荐(0)
摘要:层次遍历 思路:按层遍历每一个节点,将每一层的节点相连接 /* // Definition for a Node. class Node { public int val; public Node left; public Node right; public Node next; public N 阅读全文
posted @ 2020-10-15 09:04 消灭猕猴桃 阅读(70) 评论(0) 推荐(0)
摘要:计数 使用一个数组minfreq记录字符c在所有字符串中出现的最小频次。遍历每个字符串的字符c,使用freq 记录c在当前字符串中的最小频次,每遍历完一个字符串,将每一个minfreq[c]更新为其本身与 freq[c] 的较小值 class Solution { public List<Strin 阅读全文
posted @ 2020-10-14 11:04 消灭猕猴桃 阅读(75) 评论(0) 推荐(0)
摘要:BFS 使用一个队列,从左向右遍历每一层,将每层最右侧的节点值加入答案当中 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; 阅读全文
posted @ 2020-10-13 20:18 消灭猕猴桃 阅读(77) 评论(0) 推荐(0)
摘要:递归 遍历字符串S中的字符,如果是字母则将之前的全排列复制一份,并添加当前字母的大小写,如果不是字母则直接添加该字符 class Solution { public List<String> letterCasePermutation(String S) { List<StringBuffer> a 阅读全文
posted @ 2020-10-12 09:36 消灭猕猴桃 阅读(130) 评论(0) 推荐(0)
摘要:枚举法 由于minSize与maxSize都小于26,所以可以枚举minSize与maxSize之间的字符串,对于起始位置i和结束位置j的字符串,使用一个集合存放不同的字符。枚举每个字符时将其加入集合,如果集合的大小小于maxLetters并且并且字符串的长度在 minSize 与 maxSize 阅读全文
posted @ 2020-10-11 20:44 消灭猕猴桃 阅读(122) 评论(0) 推荐(0)
摘要:深度优先搜索 + 位运算 需要注意的是,如何去判断当前字符串是否与已经组合的字符串是否有重复。 可以使用位掩码来解决。 数字与字母映射(‘a’ -> 1, ‘b’ -> 2, ‘c’ -> 4)以此类推,如果已组合的字符位掩码与当前所选择的子字符的位掩码进行与运算后,得到的结果为0,说明无重复。 c 阅读全文
posted @ 2020-10-11 11:22 消灭猕猴桃 阅读(166) 评论(0) 推荐(0)
摘要:动态规化 class Solution { class job { int start, end, profit; job (int s, int e, int p) { start = s; end = e; profit = p; } } public int jobScheduling(int 阅读全文
posted @ 2020-10-09 19:38 消灭猕猴桃 阅读(144) 评论(0) 推荐(0)