File tree Expand file tree Collapse file tree
src/main/java/com/chen/algorithm/znn Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2222 *
2323 * @Auther: zhunn
2424 * @Date: 2020/10/24 17:23
25- * @Description: 括号生成:1-dfs
25+ * @Description: 括号生成:1-dfs(推荐)
2626 */
2727public class Solution {
2828
@@ -33,6 +33,11 @@ public List<String> generateParenthesis(int n) {
3333 return res ;
3434 }
3535
36+ /**
37+ * @param left 左边剩余的括号数
38+ * @param right 右边剩余的括号数
39+ * @param curStr 括号结果
40+ */
3641 private void dfs (int left , int right , String curStr ) {
3742 // 左右括号都不剩余了,递归终止
3843 if (left == 0 && right == 0 ) {
Original file line number Diff line number Diff line change 1818 *
1919 * @Auther: zhunn
2020 * @Date: 2020/11/2 16:13
21- * @Description: Pow(x, n):1-递归;2-迭代
21+ * @Description: Pow(x, n):1-递归;2-迭代(推荐)
2222 */
2323public class Solution {
2424
@@ -81,6 +81,26 @@ public double myPow2(double x, int n) {
8181
8282 @ Test
8383 public void test () {
84- System .out .println (myPow2 (2.0 , 5 ));
84+ System .out .println (myPow (2.0 , 10 ));
85+ }
86+
87+ public double myPow (double x , int n ) {
88+ if (n == 0 ) {
89+ return 1.0 ;
90+ }
91+ if (n < 0 ) {
92+ n = -n ;
93+ x = 1 / x ;
94+ }
95+ double res = 1d ;
96+ double x_con = x ;
97+ while (n > 0 ) {
98+ if (n % 2 == 1 ) {
99+ res = res * x_con ;
100+ }
101+ x_con = x_con * x_con ;
102+ n = n / 2 ;
103+ }
104+ return res ;
85105 }
86106}
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ public class Solution {
2424
2525 /**
2626 * 1-贪心算法
27+ * 倒序遍历,如果n-2下标能到达n-1下标,意味着前面只要有坐标能够到达n-2即可完成跳跃,于是末尾位置更改为n-2,这样构成一个子问题,迭代求解。
2728 *
2829 * @param nums
2930 * @return
@@ -35,7 +36,7 @@ public boolean canJump(int[] nums) {
3536
3637 int lastPosition = nums .length - 1 ;
3738 for (int i = nums .length - 1 ; i >= 0 ; i --) {
38- if (nums [ i ] + i >= lastPosition ) {
39+ if (i + nums [ i ] >= lastPosition ) {
3940 lastPosition = i ;
4041 }
4142 }
Original file line number Diff line number Diff line change @@ -146,21 +146,21 @@ tree
146146437. 路径之和 III *** 不需要到叶子结点返回所有路径的数量 两层递归
147147
148148recursion
149- 汉诺塔问题 HanNuoTa
150- 172. 阶乘后的零 *** (打印阶乘 Recursion)
149+ 汉诺塔问题 HanNuoTa 递归,理解记忆
150+ 172. 阶乘后的零 *** (打印阶乘 Recursion) 递归或迭代获取阶乘后结果,再取0的个数
151151
152152bfs
153153
154154dfs
155- 22. 括号生成 ***
155+ 22. 括号生成 *** 递归dfs,左右括号剩余数量,left>right剪枝, 左右括号数递减,字符串追加
156156
157157divide
158- 50. Pow(x, n) ***
159- 169. 多数元素 ***
158+ 50. Pow(x, n) *** 迭代,初始条件分类讨论判断,定义res和中间x,while(n>0)
159+ 169. 多数元素 *** 1-排序;2-哈希表(推荐)放入hashmap计数,判断数量是否大于len/2
160160
161161greedy
162- 122. 买卖股票的最佳时机 II ***
163- 55. 跳跃游戏
162+ 122. 买卖股票的最佳时机 II *** 贪心-迭代计算只要有收益就累加到max,返回max
163+ 55. 跳跃游戏 贪心-倒序遍历,如果n-2下标能到达n-1下标,意味着前面只要有坐标能够到达n-2即可完成跳跃,于是末尾位置更改为n-2,这样构成一个子问题,迭代求解。
164164
165165
166166backtrack
You can’t perform that action at this time.
0 commit comments