![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
每日打卡&周赛
算法每日打卡&周赛
carroll18
你想要拥有你从未有过的东西,你必须去做你从未做过的事情。
展开
-
[leetcode]二叉树的遍历问题
原创 2022-05-11 14:22:03 · 158 阅读 · 0 评论 -
力扣---2020.10.16
977. 有序数组的平方class Solution { public int[] sortedSquares(int[] A) { int len = A.length; int [] arr = new int[len]; for(int i =0 ; i<len; i++){ arr[i] = A[i]*A[i]; } Arrays.sort(arr); return原创 2021-04-18 22:05:28 · 122 阅读 · 0 评论 -
力扣---2020.10.15
116. 填充每个节点的下一个右侧节点指针class Solution { public Node connect(Node root) { if(root == null || root.left == null) return root; root.left.next = root.right; if(root.next != null){ root.right.next = root.next.left;原创 2021-04-11 19:44:41 · 133 阅读 · 0 评论 -
力扣---2020.10.14
1002. 查找常用字符class Solution { public List<String> commonChars(String[] A) { List<String> list = new ArrayList<>(); int[] res = new int[26]; for(char c : A[0].toCharArray()){ res[c-'a']++; }原创 2021-04-10 17:10:57 · 119 阅读 · 0 评论 -
力扣---2020.10.13
24. 两两交换链表中的节点class Solution { public ListNode swapPairs(ListNode head) { if(head == null || head.next == null){ return head; } ListNode next = head.next; head.next = swapPairs(next.next); next.next原创 2021-04-06 16:14:13 · 131 阅读 · 0 评论 -
力扣---2020.10.12
530. 二叉搜索树的最小绝对差class Solution { int min = Integer.MAX_VALUE; TreeNode preNode = null; public int getMinimumDifference(TreeNode root) { if(root==null){ return 0; } getMin(root); return min; }原创 2021-04-05 14:44:41 · 112 阅读 · 2 评论 -
力扣---2020.10.11
416. 分割等和子集class Solution { public boolean canPartition(int[] nums) { if(nums == null || nums.length == 0){ return true; } int sum = 0; for(int i = 0;i < nums.length;i++){ sum += nums[i];原创 2021-03-30 14:49:19 · 130 阅读 · 0 评论 -
力扣---2020.10.10
142. 环形链表 IIpublic class Solution { public ListNode detectCycle(ListNode head) { ListNode pos = head; Set<ListNode> visited = new HashSet<ListNode>(); while (pos != null) { if (visited.contains(pos)) {原创 2020-12-18 10:17:56 · 198 阅读 · 0 评论 -
力扣---2020.10.9
141. 环形链表public class Solution { public boolean hasCycle(ListNode head) { if(head == null || head.next == null){ return false; } ListNode fast = head.next; ListNode slow = head; while(fast != slow){原创 2020-12-18 10:17:33 · 183 阅读 · 0 评论 -
力扣---2020.10.8
344. 反转字符串class Solution { public void reverseString(char[] s) { int left = 0,right = s.length-1; while(left<right){ char c = s[left]; s[left] = s[right]; s[right] = c; left++;原创 2020-12-18 10:17:15 · 194 阅读 · 0 评论 -
力扣---2020.10.7
75. 颜色分类class Solution { public void sortColors(int[] nums) { int n = nums.length; int ptr = 0; for (int i = 0; i < n; ++i) { if (nums[i] == 0) { int temp = nums[i]; nums[i] = nums原创 2020-12-18 08:13:22 · 143 阅读 · 0 评论 -
力扣---2020.10.6
834. 树中距离之和class Solution { int[] ans; int[] sz; int[] dp; List<List<Integer>> graph; public int[] sumOfDistancesInTree(int N, int[][] edges) { ans = new int[N]; sz = new int[N]; dp = new int[N];原创 2020-12-18 08:12:57 · 152 阅读 · 0 评论 -
力扣---2020.10.5
18. 四数之和// 超时了.......我好菜啊class Solution { List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> fourSum(int[] nums, int target) { Arrays.sort(nums); backtrack(nums,target,new ArrayLi原创 2020-12-18 08:12:31 · 156 阅读 · 0 评论 -
力扣---2020.10.4
2. 两数相加class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int carry = 0; ListNode dummy = new ListNode(0); ListNode re = dummy; while(l1!=null || l2!=null || carry!=0){ int sum = 0;原创 2020-12-18 08:11:55 · 189 阅读 · 0 评论 -
力扣---2020.9.27
235. 二叉搜索树的最近公共祖先class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (p.val==root.val){ return p; } if (q.val==root.val){ return q; } if (p.原创 2020-12-17 08:47:51 · 162 阅读 · 0 评论 -
力扣---2020.9.24
501. 二叉搜索树中的众数class Solution { int preVal = 0, curTimes = 0, maxTimes = 0; ArrayList<Integer> list = new ArrayList<Integer>(); public int[] findMode(TreeNode root) { traversal(root); //list转换为int[] int size = list.size(); int[]原创 2020-12-16 18:00:27 · 132 阅读 · 0 评论 -
力扣---2020.9.30
701. 二叉搜索树中的插入操作class Solution { public TreeNode insertIntoBST(TreeNode root, int val) { if(root == null){ return new TreeNode(val); } if(root.val < val){ root.right = insertIntoBST(root.right,val);原创 2020-12-17 08:48:50 · 85 阅读 · 0 评论 -
力扣---2020.9.29
145. 二叉树的后序遍历class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<Integer>(); postorder(root, res); return res; } public void postorder(TreeNode原创 2020-12-17 08:48:19 · 174 阅读 · 0 评论 -
力扣---2020.10.1
LCP 19. 秋叶收藏集class Solution { public int minimumOperations(String leaves) { int n = leaves.length(); int g = leaves.charAt(0) == 'y' ? 1 : -1; int gmin = g; int ans = Integer.MAX_VALUE; for (int i = 1; i < n;原创 2020-12-18 08:10:41 · 146 阅读 · 0 评论 -
力扣---2020.10.2
771. 宝石与石头class Solution { public int numJewelsInStones(String J, String S) { if (S == null || S.isEmpty()) return 0; if (J == null || J.isEmpty()) return 0; byte[] arr = new byte[58]; int count = 0; for (char原创 2020-12-18 08:11:09 · 133 阅读 · 0 评论 -
力扣---2020.10.3
1. 两数之和class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> map = new HashMap<>(); for(int i = 0;i < nums.length;i++){ if(map.containsKey(target-nums[i])){ retur原创 2020-12-18 08:11:29 · 151 阅读 · 0 评论 -
力扣---2020.9.28
117. 填充每个节点的下一个右侧节点指针 IIclass Solution { public Node connect(Node root) { if(root == null){ return null; } if(root.left != null && root.right != null){ root.left.next = root.right; }原创 2020-12-17 08:47:17 · 202 阅读 · 0 评论 -
力扣---2020.9.26
113. 路径总和 IIclass Solution { List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> pathSum(TreeNode root, int sum) { dfs(root,sum,new ArrayList<Integer>()); return res; }原创 2020-12-16 18:01:32 · 86 阅读 · 0 评论 -
力扣---2020.9.25
106. 从中序与后序遍历序列构造二叉树class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { return helper(inorder, postorder, postorder.length - 1, 0, inorder.length - 1); } public TreeNode helper(int[] inorder, int[] postorder,原创 2020-12-16 18:01:16 · 131 阅读 · 0 评论 -
力扣---2020.9.23
617. 合并二叉树class Solution { public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { if(t1 == null){ return t2; } if(t2 == null){ return t1; } t1.val += t2.val; t1.left = mergeTrees原创 2020-12-16 18:00:05 · 136 阅读 · 0 评论 -
力扣---2020.9.22
968. 监控二叉树class Solution { private int ans = 0; public int minCameraCover(TreeNode root) { if (root == null) return 0; if (dfs(root) == 2) ans++; return ans; } // 1:该节点安装了监视器 2:该节点可观,但没有安装监视器 3:该节点不可观原创 2020-12-16 17:59:17 · 153 阅读 · 0 评论 -
力扣---2020.9.21
538. 把二叉搜索树转换为累加树class Solution { int sum = 0; public TreeNode convertBST(TreeNode root) { if(root==null) return null; convertBST(root.right); root.val += sum; sum = root.val; convertBST(root.left);原创 2020-12-16 17:59:01 · 136 阅读 · 0 评论 -
力扣---2020.9.20
78. 子集class Solution { List<List<Integer>> res = new ArrayList<>(); boolean[] used; public List<List<Integer>> subsets(int[] nums) { used = new boolean[nums.length]; backtrack(nums,new ArrayList<原创 2020-12-16 17:58:43 · 144 阅读 · 0 评论 -
力扣---2020.9.19
404. 左叶子之和class Solution { public int sumOfLeftLeaves(TreeNode root) { if(root==null) return 0; int sum = 0; if(root.left != null && root.left.left == null && root.left.right==null){ sum += root.left原创 2020-12-16 17:58:26 · 116 阅读 · 0 评论 -
力扣---2020.9.18
47. 全排列 IIclass Solution { List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> permuteUnique(int[] nums) { Arrays.sort(nums); boolean[] used = new boolean[nums.length]; List原创 2020-12-16 09:23:29 · 164 阅读 · 0 评论 -
力扣---2020.9.17
685. 冗余连接 IIclass Solution { int[] anc;//并查集 int[] parent;// record the father of every node to find the one with 2 fathers,记录每个点的父亲,为了找到双入度点 public int[] findRedundantDirectedConnection(int[][] edges) { anc=new int[edges.length+1];原创 2020-12-16 09:23:06 · 154 阅读 · 0 评论 -
力扣---2020.9.16
226. 翻转二叉树class Solution { public TreeNode invertTree(TreeNode root) { if(root==null) return root; TreeNode temp = invertTree(root.left); root.left = invertTree(root.right); root.right = temp; return root; }原创 2020-12-14 18:23:17 · 126 阅读 · 0 评论 -
力扣---2020.9.15
37. 解数独class Solution { public void solveSudoku(char[][] board) { backtrack(board,0,0); } public boolean backtrack(char[][] board,int i,int j){ int m = 9,n = 9; if(i==m){ return true; } if(原创 2020-12-14 18:22:43 · 136 阅读 · 0 评论 -
力扣---2020.9.14
94. 二叉树的中序遍历class Solution { List<Integer> res = new ArrayList<>(); public List<Integer> inorderTraversal(TreeNode root) { if(root==null) return res; inorderTraversal(root.left); res.add(root.val);原创 2020-12-10 08:44:07 · 144 阅读 · 0 评论 -
力扣---2020.9.13
79. 单词搜索class Solution { public boolean exist(char[][] board, String word) { int m = board.length; int n = board[0].length; for(int i = 0;i < m;i++){ for(int j = 0;j < n;j++){ if(dfs(board,word原创 2020-12-10 08:43:34 · 156 阅读 · 0 评论 -
力扣---2020.9.12
637. 二叉树的层平均值//BFSclass Solution { public List<Double> averageOfLevels(TreeNode root) { List<Double> res = new ArrayList<>(); Queue<TreeNode> queue = new LinkedList<>(); if(root != null){原创 2020-12-10 08:43:04 · 157 阅读 · 0 评论 -
力扣---2020.9.11
216. 组合总和 IIIclass Solution { List<List<Integer>> lists = new ArrayList<>(); public List<List<Integer>> combinationSum3(int k, int n) { List<Integer> list = new ArrayList<>(); dfs(k,1,n,lis原创 2020-12-10 08:42:28 · 144 阅读 · 0 评论 -
力扣---2020.9.10
40. 组合总和 IIclass Solution { List<List<Integer>> list = new ArrayList<>(); public List<List<Integer>> combinationSum2(int[] candidates, int target) { Arrays.sort(candidates); backtrack(candidates,new Ar原创 2020-12-09 08:53:44 · 140 阅读 · 0 评论 -
力扣---2020.9.9
39. 组合总和class Solution { List<List<Integer>> list = new ArrayList<>(); public List<List<Integer>> combinationSum(int[] candidates, int target) { Arrays.sort(candidates); backtrack(candidates,new ArrayL原创 2020-12-09 08:53:21 · 149 阅读 · 0 评论 -
力扣---2020.9.8
77. 组合class Solution { List<List<Integer>> list = new ArrayList<>(); public List<List<Integer>> combine(int n, int k) { backtrack(n,k,new ArrayList<>(),1); return list; } public void bac原创 2020-12-06 11:58:01 · 210 阅读 · 0 评论