随笔分类 -  LeetCode

上一页 1 2 3 4 5 6 7 ··· 12 下一页
摘要:深度优先搜索 class Solution { public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { /** * 如果某棵树为空,则返回另一棵树 */ if (root1 == null){ return root2; } if ( 阅读全文
posted @ 2022-02-22 21:51 振袖秋枫问红叶 阅读(29) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public TreeNode constructMaximumBinaryTree(int[] nums) { return buildSonTree(nums, 0, nums.length); } /** * 左闭右开区间 * 先找到数组的最大值 阅读全文
posted @ 2022-02-22 21:27 振袖秋枫问红叶 阅读(28) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { return buildSonTree(preorder, 0, preorder.length, inorder, 0, inord 阅读全文
posted @ 2022-02-22 21:14 振袖秋枫问红叶 阅读(40) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { return buildSonTree(inorder, 0, inorder.length, postorder, 0, post 阅读全文
posted @ 2022-02-22 20:50 振袖秋枫问红叶 阅读(44) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public int findBottomLeftValue(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); int res = 0; queue.add(root); whil 阅读全文
posted @ 2022-02-22 16:06 振袖秋枫问红叶 阅读(22) 评论(0) 推荐(0)
摘要:深度优先搜索 /** * 时间复杂度 O(n) * 空间复杂度 O(logn) */class Solution { public int maxDepth(Node root) { if (root == null){ return 0; } int max = 0; for (Node c : 阅读全文
posted @ 2022-02-21 17:54 振袖秋枫问红叶 阅读(26) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public boolean isSubtree(TreeNode root, TreeNode subRoot) { /** * 对于树的每个节点,都判断一下是否和子树是一个树(《100. 相同的树》) */ if (subRoot == null) 阅读全文
posted @ 2022-02-21 17:11 振袖秋枫问红叶 阅读(25) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { List<Integer> list = new LinkedList<>(); public List<Integer> postorder(Node root) { if (root == null){ return list; } for (No 阅读全文
posted @ 2022-02-21 14:52 振袖秋枫问红叶 阅读(21) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { List<Integer> list = new LinkedList<>(); public List<Integer> preorder(Node root) { if (root == null){ return list; } list.add 阅读全文
posted @ 2022-02-21 14:40 振袖秋枫问红叶 阅读(20) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public Node connect(Node root) { Queue<Node> queue = new LinkedList<>(); if (root == null){ return root; } queue.add(root); wh 阅读全文
posted @ 2022-02-21 13:54 振袖秋枫问红叶 阅读(25) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public Node connect(Node root) { Queue<Node> queue = new LinkedList<>(); if (root == null){ return root; } queue.add(root); wh 阅读全文
posted @ 2022-02-21 13:50 振袖秋枫问红叶 阅读(26) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public List<Integer> largestValues(TreeNode root) { LinkedList<Integer> list = new LinkedList<>(); Queue<TreeNode> queue = new 阅读全文
posted @ 2022-02-21 13:05 振袖秋枫问红叶 阅读(30) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public List<List<Integer>> levelOrder(Node root) { List<List<Integer>> list = new LinkedList<>(); Queue<Node> queue = new Link 阅读全文
posted @ 2022-02-21 12:05 振袖秋枫问红叶 阅读(29) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public List<Double> averageOfLevels(TreeNode root) { List<Double> list = new LinkedList<>(); Queue<TreeNode> queue = new Linke 阅读全文
posted @ 2022-02-21 11:46 振袖秋枫问红叶 阅读(24) 评论(0) 推荐(0)
摘要:双端队列实现单调队列 import java.util.ArrayDeque; class Solution { public int[] maxSlidingWindow(int[] nums, int k) { /** * 双端队列实现单调队列 * 队列存储的是元素的索引 */ Deque<In 阅读全文
posted @ 2022-02-20 11:26 振袖秋枫问红叶 阅读(35) 评论(0) 推荐(0)
摘要:栈 import java.util.Stack; class Solution { public String removeDuplicates(String s) { Stack<Character> stack = new Stack(); for (int i = 0; i < s.leng 阅读全文
posted @ 2022-02-19 22:48 振袖秋枫问红叶 阅读(54) 评论(0) 推荐(0)
摘要:class Solution { public boolean repeatedSubstringPattern(String s) { /** * 将一个字符串复制两份,然后去掉首尾元素,如果剩余的子串中还包含原字符串,说明原字符串含有重复的子串 */ String str = s + s; re 阅读全文
posted @ 2022-02-19 20:57 振袖秋枫问红叶 阅读(32) 评论(0) 推荐(0)
摘要:双指针 class Solution { public int strStr(String haystack, String needle) { /** * 如果needle为空,结果肯定为0 * 如果needle不为空,如果haystack为空,结果肯定为-1 */ if (needle.leng 阅读全文
posted @ 2022-02-19 20:44 振袖秋枫问红叶 阅读(24) 评论(0) 推荐(0)
摘要:双指针 class Solution { public String reverseLeftWords(String s, int n) { StringBuilder str = new StringBuilder(s); /** * 分别反转前n个字符和剩余字符,然后反转全部字符 */ reve 阅读全文
posted @ 2022-02-19 20:05 振袖秋枫问红叶 阅读(26) 评论(0) 推荐(0)
摘要:双指针 class Solution { public String reverseWords(String s) { StringBuilder str = new StringBuilder(); int left = 0; int right = s.length() - 1; /** * 去 阅读全文
posted @ 2022-02-19 19:43 振袖秋枫问红叶 阅读(27) 评论(0) 推荐(0)

上一页 1 2 3 4 5 6 7 ··· 12 下一页