Skip to content

Commit 2b1138c

Browse files
committed
add q70/q104
1 parent 1991c8b commit 2b1138c

File tree

5 files changed

+69
-17
lines changed

5 files changed

+69
-17
lines changed

.idea/workspace.xml

Lines changed: 19 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969

7070
* [q21_合并两个有序链表](/src/递归/q21_合并两个有序链表)
7171
* [q101_对称二叉树](/src/递归/q101_对称二叉树)
72+
* [q104_二叉树的最大深度](/src/递归/q104_二叉树的最大深度)
7273
* [q226_翻转二叉树](/src/递归/q226_翻转二叉树)
7374
* [q236_二叉树的最近公共祖先](/src/递归/q236_二叉树的最近公共祖先)
7475

@@ -81,6 +82,7 @@
8182

8283
* [q5_最长回文子串](/src/动态规划/q5_最长回文子串)
8384
* [q53_最大子序和](/src/动态规划/q53_最大子序和)
85+
* [q70_爬楼梯](/src/动态规划/q70_爬楼梯)
8486
* [q118_杨辉三角](/src/动态规划/q118_杨辉三角)
8587
* [q300_最长上升子序列](/src/动态规划/q300_最长上升子序列)
8688
* [q746_使用最小花费爬楼梯](/src/动态规划/q746_使用最小花费爬楼梯)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package 动态规划.q70_爬楼梯;
2+
3+
/**
4+
* 动态规划 dp[i]表示到达第i阶的方法总数dp[i]=dp[i−1]+dp[i−2] o(n)
5+
*/
6+
public class Solution {
7+
8+
public int climbStairs(int n) {
9+
if (n == 1) {
10+
return 1;
11+
}
12+
int[] dp = new int[n + 1];
13+
dp[1] = 1;
14+
dp[2] = 2;
15+
for (int i = 3; i <= n; i++) {
16+
dp[i] = dp[i - 1] + dp[i - 2];
17+
}
18+
return dp[n];
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package 递归.q104_二叉树的最大深度;
2+
3+
/**
4+
* 递归 o(n)
5+
*/
6+
public class Solution {
7+
8+
public int maxDepth(TreeNode root) {
9+
if (root == null) {
10+
return 0;
11+
} else {
12+
int leftHeight = maxDepth(root.left);
13+
int rightHeight = maxDepth(root.right);
14+
return Math.max(leftHeight, rightHeight) + 1;
15+
}
16+
}
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package 递归.q104_二叉树的最大深度;
2+
3+
public class TreeNode {
4+
int val;
5+
TreeNode left;
6+
TreeNode right;
7+
8+
TreeNode(int x) {
9+
val = x;
10+
}
11+
}

0 commit comments

Comments
 (0)