随笔分类 - 刷题笔记
摘要:https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/ 最坏的时间复杂度是O(n)的 思路: 先把后面的末尾与前面的开始的那段去掉(因为可能有重复的数字) 处理数组完全单调的特殊情况:去掉重复的之后num
阅读全文
摘要:若干方法: https://www.acwing.com/blog/content/25/
阅读全文
摘要:以前序遍历为基准,找到root 把中序遍历当工具,找到root的位置,以此找出原树中左子树的数目,右子树的数目 然后再使用递归左右子树 class Solution { int [] preorder; HashMap<Integer, Integer> dic = new HashMap<>();
阅读全文
摘要:开辟新字符串 在java中,字符串是不可变类型,无法原地修改,所以需要开辟一个新的字符串,遍历原字符串,不是空格直接添加,如果是空格的话,直接添加“%20” class Solution { public String replaceSpace(String s) { StringBuilder r
阅读全文
摘要:原地交换(优解) 遍历数组,如果存在某个数不在0到n-1的范围内,返回-1。 每个数放到对应的位置上,即让 nums[i] = i 从前往后遍历数组中的所有数 如果对应的值不等于所在地址的值,交换值到对应的地址上,即:nums = x 时间复杂度分析 每次swap操作都会将一个数放在正确的位置上,最
阅读全文
摘要:递归 class Solution { public boolean isValidBST(TreeNode root) { return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE); } public boolean isValidBST(Tr
阅读全文
摘要:排序(优解) 先排序,选第一个数,用两个指针分别指向剩下的第一个数和最后的数,相加比较,大的话动二指针,小的话动一指针,如果重合还没有那么就返回 class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sor
阅读全文
摘要:排序 按照字典的顺序排序,再比较排序好的单词 O(n*logn) 哈希 O(n) 使用数组映射(性能更好,本质也是哈希表) class Solution { public boolean isAnagram(String s, String t) { if (s.length() != t.leng
阅读全文
摘要:https://mp.weixin.qq.com/s/FFsvWXiaZK96PtUg-mmtEw 703. 数据流中的第 K 大元素 https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/ class KthLargest
阅读全文
摘要:https://leetcode-cn.com/problems/valid-parentheses/
阅读全文
摘要:哈希表 遍历将结点存储在哈希表中,如果遇到环的入口,会检测到 public class Solution { public boolean hasCycle(ListNode head) { Set<ListNode> seen = new HashSet<ListNode>(); while (h
阅读全文
摘要:链表 反转链表 https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/ https://www.cnblogs.com/zhbeii/p/15422709.html 两两交换相邻 https://leetcode-cn.com/probl
阅读全文
摘要:使用递归 使用的这个递归又把我弄糊涂了,只能是暂时理解这4个,如果把他想成一个整体的话,有head、next,还有一个后续的结点,前面还是有一个 class Solution { public ListNode swapPairs(ListNode head) { if(head == null |
阅读全文
摘要:遍历路径 遍历两个结点向上的路径,看第一个重合的结点 遍历全部 遍历整个树, class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { while(root != nu
阅读全文
摘要:数组越界: head为空指针; k大于链表的长度; 输入的参数k为0; public class Solution { public ListNode FindKthToTail (ListNode head, int k) { ListNode slow = head, fast = head;
阅读全文
摘要:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 使用滑动窗口,max会一直更新结束之前的最长的长度, 在最初的时候每一次遇到一个键,都会添加到map,后面如果遇到重复的,就会把走过的相同
阅读全文
摘要:https://leetcode-cn.com/problems/container-with-most-water/ 使用双指针 class Solution { public int maxArea(int[] height) { int i = 0, j = height.length - 1
阅读全文
摘要:https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/ 思路: int[][]的时候,二维数组.length返回的是数组的行数 把这个转化成图,搜索二叉树 class Solution { public boole
阅读全文
摘要:https://leetcode-cn.com/problems/single-number/ 任何数和 00 做异或运算,结果仍然是原来的数,即 a \oplus 0=aa⊕0=a。 任何数和其自身做异或运算,结果是 00,即 a \oplus a=0a⊕a=0。 异或运算满足交换律和结合律 cl
阅读全文