Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Week_01/id_9/LeetCode_104_9.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
39 changes: 39 additions & 0 deletions Week_01/id_9/LeetCode_236_9.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
33 changes: 33 additions & 0 deletions Week_01/id_9/LeetCode_26_9.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
46 changes: 46 additions & 0 deletions Week_01/id_9/LeetCode_50_9.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 2 additions & 0 deletions Week_01/id_9/NOTE.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# 学习笔记

> 在 LeetCode 刷题的过程中,可对部分题进行变形简化处理逻辑。
Binary file added Week_01/id_9/数据结构与算法脑图.xmind
Binary file not shown.