diff --git a/Week_01/id_9/LeetCode_104_9.java b/Week_01/id_9/LeetCode_104_9.java new file mode 100644 index 00000000..3b4b762e --- /dev/null +++ b/Week_01/id_9/LeetCode_104_9.java @@ -0,0 +1,39 @@ +package com.github.lifelab.leetcode.problemset; + +/** + * 二叉树的最大深度 @https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ + * + * @author Weichao Li (liweichao0102@gmail.com) + * @since 2019-06-03 + */ +public class Solution104 { + + public int maxDepth(TreeNode root) { + return depth(root, 0); + } + + private int depth(TreeNode node, int depth) { + + if (node == null) { + return 0; + } + + depth++; + + if (node.left == null && node.right == null) { + return depth; + } + + int l = 0, r = 0; + + if (node.left != null) { + l = depth(node.left, depth); + } + + if (node.right != null) { + r = depth(node.right, depth); + } + + return Math.max(l, r); + } +} diff --git a/Week_01/id_9/LeetCode_236_9.java b/Week_01/id_9/LeetCode_236_9.java new file mode 100644 index 00000000..19b5f34a --- /dev/null +++ b/Week_01/id_9/LeetCode_236_9.java @@ -0,0 +1,39 @@ +package com.github.lifelab.leetcode.problemset; + +/** + * 二叉树的最近公共祖先 @https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/ + * + * @author Weichao Li (liweichao0102@gmail.com) + * @since 2019-06-09 + */ +public class Solution236 { + + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + + if (root == null) { + return null; + } + + if (root.val == p.val || root.val == q.val) { + return root; + } + + TreeNode leftVal = lowestCommonAncestor(root.left, p, q); + + TreeNode rightVal = lowestCommonAncestor(root.right, p, q); + + if (rightVal != null && leftVal != null) { + return root; + } + + if (rightVal != null) { + return rightVal; + } + + if (leftVal != null) { + return leftVal; + } + return null; + } + +} diff --git a/Week_01/id_9/LeetCode_26_9.java b/Week_01/id_9/LeetCode_26_9.java new file mode 100644 index 00000000..0dd92c4c --- /dev/null +++ b/Week_01/id_9/LeetCode_26_9.java @@ -0,0 +1,33 @@ +package com.github.lifelab.leetcode.problemset; + +/** + * 删除排序数组中的重复项 @https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/ + * + * @author weichao.li (liweichao0102@gmail.com) + * @since 2019-06-04 + */ +public class Solution26 { + + /** + * 1 1 1 2 3 4 4 4 + * + * @param nums + * @return + * @ + */ + public int removeDuplicates(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } + int left = 0; + for (int i = 1; i < nums.length; i++) { + if (nums[left] != nums[i]) { + left++; + nums[left] = nums[i]; + } + } + return left + 1; + } + + +} \ No newline at end of file diff --git a/Week_01/id_9/LeetCode_50_9.java b/Week_01/id_9/LeetCode_50_9.java new file mode 100644 index 00000000..850ff8d9 --- /dev/null +++ b/Week_01/id_9/LeetCode_50_9.java @@ -0,0 +1,46 @@ +package com.github.lifelab.leetcode.problemset; + +/** + * Pow(x, n) @https://leetcode-cn.com/problems/powx-n/ + * + * @author Weichao Li (liweichao0102@gmail.com) + * @since 2019-06-06 + */ +public class Solution50 { + + public double myPow(double x, int n) { + + if (n < 0) { + return 1 / halfPow(x, -n); + } else { + return halfPow(x, n); + } + + } + + public double halfPow(double x, int n) { + + //terminator + if (n == 0) { + return 1; + } + if (n == 1) { + return x; + } + //process && drill down + + double result; + + boolean flag = n % 2 == 0; + + double temp = halfPow(x, n / 2); + + if (flag) { + result = temp * temp; + } else { + result = temp * temp * x; + } + + return result; + } +} \ No newline at end of file diff --git a/Week_01/id_9/NOTE.md b/Week_01/id_9/NOTE.md index 107ea7d6..01582ef8 100644 --- a/Week_01/id_9/NOTE.md +++ b/Week_01/id_9/NOTE.md @@ -1 +1,3 @@ # 学习笔记 + +> 在 LeetCode 刷题的过程中,可对部分题进行变形简化处理逻辑。 \ No newline at end of file diff --git "a/Week_01/id_9/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225\350\204\221\345\233\276.xmind" "b/Week_01/id_9/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225\350\204\221\345\233\276.xmind" new file mode 100644 index 00000000..6005da3b Binary files /dev/null and "b/Week_01/id_9/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225\350\204\221\345\233\276.xmind" differ