随笔分类 - LeetCode
摘要:深度优先搜索 class Solution { public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { /** * 如果某棵树为空,则返回另一棵树 */ if (root1 == null){ return root2; } if (
阅读全文
摘要:深度优先搜索 class Solution { public TreeNode constructMaximumBinaryTree(int[] nums) { return buildSonTree(nums, 0, nums.length); } /** * 左闭右开区间 * 先找到数组的最大值
阅读全文
摘要:深度优先搜索 class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { return buildSonTree(preorder, 0, preorder.length, inorder, 0, inord
阅读全文
摘要:深度优先搜索 class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { return buildSonTree(inorder, 0, inorder.length, postorder, 0, post
阅读全文
摘要:广度优先搜索 class Solution { public int findBottomLeftValue(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); int res = 0; queue.add(root); whil
阅读全文
摘要:深度优先搜索 /** * 时间复杂度 O(n) * 空间复杂度 O(logn) */class Solution { public int maxDepth(Node root) { if (root == null){ return 0; } int max = 0; for (Node c :
阅读全文
摘要:深度优先搜索 class Solution { public boolean isSubtree(TreeNode root, TreeNode subRoot) { /** * 对于树的每个节点,都判断一下是否和子树是一个树(《100. 相同的树》) */ if (subRoot == null)
阅读全文
摘要:深度优先搜索 class Solution { List<Integer> list = new LinkedList<>(); public List<Integer> postorder(Node root) { if (root == null){ return list; } for (No
阅读全文
摘要:深度优先搜索 class Solution { List<Integer> list = new LinkedList<>(); public List<Integer> preorder(Node root) { if (root == null){ return list; } list.add
阅读全文
摘要:广度优先搜索 class Solution { public Node connect(Node root) { Queue<Node> queue = new LinkedList<>(); if (root == null){ return root; } queue.add(root); wh
阅读全文
摘要:广度优先搜索 class Solution { public Node connect(Node root) { Queue<Node> queue = new LinkedList<>(); if (root == null){ return root; } queue.add(root); wh
阅读全文
摘要:广度优先搜索 class Solution { public List<Integer> largestValues(TreeNode root) { LinkedList<Integer> list = new LinkedList<>(); Queue<TreeNode> queue = new
阅读全文
摘要:广度优先搜索 class Solution { public List<List<Integer>> levelOrder(Node root) { List<List<Integer>> list = new LinkedList<>(); Queue<Node> queue = new Link
阅读全文
摘要:广度优先搜索 class Solution { public List<Double> averageOfLevels(TreeNode root) { List<Double> list = new LinkedList<>(); Queue<TreeNode> queue = new Linke
阅读全文
摘要:双端队列实现单调队列 import java.util.ArrayDeque; class Solution { public int[] maxSlidingWindow(int[] nums, int k) { /** * 双端队列实现单调队列 * 队列存储的是元素的索引 */ Deque<In
阅读全文
摘要:栈 import java.util.Stack; class Solution { public String removeDuplicates(String s) { Stack<Character> stack = new Stack(); for (int i = 0; i < s.leng
阅读全文
摘要:class Solution { public boolean repeatedSubstringPattern(String s) { /** * 将一个字符串复制两份,然后去掉首尾元素,如果剩余的子串中还包含原字符串,说明原字符串含有重复的子串 */ String str = s + s; re
阅读全文
摘要:双指针 class Solution { public int strStr(String haystack, String needle) { /** * 如果needle为空,结果肯定为0 * 如果needle不为空,如果haystack为空,结果肯定为-1 */ if (needle.leng
阅读全文
摘要:双指针 class Solution { public String reverseLeftWords(String s, int n) { StringBuilder str = new StringBuilder(s); /** * 分别反转前n个字符和剩余字符,然后反转全部字符 */ reve
阅读全文
摘要:双指针 class Solution { public String reverseWords(String s) { StringBuilder str = new StringBuilder(); int left = 0; int right = s.length() - 1; /** * 去
阅读全文

浙公网安备 33010602011771号