File tree Expand file tree Collapse file tree 6 files changed +74
-5
lines changed
project/LeetCode/leetcode/src/main/java/com/blankj/easy Expand file tree Collapse file tree 6 files changed +74
-5
lines changed Original file line number Diff line number Diff line change 2121| 53| [ Maximum Subarray] [ 053 ] | Array, Dynamic Programming, Divide and Conquer|
2222| 58| [ Length of Last Word] [ 058 ] | String|
2323| 66| [ Plus One] [ 066 ] | Array, Math|
24+ | 67| [ Add Binary] [ 067 ] | Math, String|
2425| 69| [ Sqrt(x)] [ 069 ] | Binary Search, Math|
26+ | 70| [ Climbing Stairs] [ 070 ] | Dynamic Programming|
2527
2628
2729## Medium
6163[ 053 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/053/README.md
6264[ 058 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/058/README.md
6365[ 066 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/066/README.md
66+ [ 067 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/067/README.md
6467[ 069 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/069/README.md
68+ [ 070 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/070/README.md
Original file line number Diff line number Diff line change 1- # [ Plus One ] [ title ]
1+ # [ Add Binary ] [ title ]
22
33## Description
44
@@ -14,7 +14,7 @@ Return `"100"`.
1414
1515## 思路
1616
17- 题意是给你两个二进制串,求其和的二进制串。
17+ 题意是给你两个二进制串,求其和的二进制串。我们就按照小学算数那么来做,用 ` carry ` 表示进位,从后往前算,依次往前,每算出一位就插入到最前面即可,直到把两个二进制串都遍历完即可。
1818
1919``` java
2020public class Solution {
@@ -52,5 +52,5 @@ public class Solution {
5252
5353
5454
55- [ title ] : https://leetcode.com/problems/plus-one
55+ [ title ] : https://leetcode.com/problems/add-binary
5656[ ajl ] : https://github.com/Blankj/awesome-java-leetcode
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ Compute and return the square root of x.
1111
1212## 思路
1313
14- 题意是求平方根,参考[ 牛顿迭代法求平方根] ( https://wenku.baidu.com/view/6b74c622bcd126fff7050bfe.html ) ,我们就可以高效低实现了 。
14+ 题意是求平方根,参考[ 牛顿迭代法求平方根] ( https://wenku.baidu.com/view/6b74c622bcd126fff7050bfe.html ) ,然后再参考维基百科的 [ Integer square root ] ( https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division ) 即可 。
1515
1616``` java
1717public class Solution {
Original file line number Diff line number Diff line change 1+ # [ Climbing Stairs] [ title ]
2+
3+ ## Description
4+
5+ You are climbing a stair case. It takes * n* steps to reach to the top.
6+
7+ Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
8+
9+ ** Note:** Given * n* will be a positive integer.
10+
11+ ** Tags:** Binary Search, Math
12+
13+
14+ ## 思路
15+
16+ 题意是爬楼梯,每次你只能爬一步或者两步,问到顶层共有多少种方案。我们假设到顶层共有` f(n) ` 种,那么` f(n) = f(n - 1) + f(n - 2) ` 肯定是成立的,意思就是我们迈向顶层的最后一步是在倒数第一级台阶或者在倒数第二级台阶。算法我对空间复杂度进行了优化,因为在迭代过程中只需要两个变量即可。
17+
18+ ``` java
19+ public class Solution {
20+ public int climbStairs (int n ) {
21+ int a = 1 , b = 1 ;
22+ while (-- n > 0 ) {
23+ b += a;
24+ a = b - a;
25+ }
26+ return b;
27+ }
28+ }
29+ ```
30+
31+
32+ ## 结语
33+
34+ 如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[ awesome-java-leetcode] [ ajl ]
35+
36+
37+
38+ [ title ] : https://leetcode.com/problems/climbing-stairs
39+ [ ajl ] : https://github.com/Blankj/awesome-java-leetcode
Original file line number Diff line number Diff line change @@ -20,6 +20,6 @@ public int mySqrt(int x) {
2020
2121 public static void main (String [] args ) {
2222 Solution solution = new Solution ();
23- System .out .println (solution .mySqrt (11 ));
23+ System .out .println (solution .mySqrt (10 ));
2424 }
2525}
Original file line number Diff line number Diff line change 1+ package com .blankj .easy ._070 ;
2+
3+ /**
4+ * <pre>
5+ * author: Blankj
6+ * blog : http://blankj.com
7+ * time : 2017/05/09
8+ * desc :
9+ * </pre>
10+ */
11+
12+ public class Solution {
13+ public int climbStairs (int n ) {
14+ int a = 1 , b = 1 ;
15+ while (--n > 0 ) {
16+ b += a ;
17+ a = b - a ;
18+ }
19+ return b ;
20+ }
21+
22+ public static void main (String [] args ) {
23+ Solution solution = new Solution ();
24+ System .out .println (solution .climbStairs (3 ));
25+ }
26+ }
You can’t perform that action at this time.
0 commit comments