Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1d37c98

Browse files
committed
docs: 新增No20和No70题解
1 parent 212cb37 commit 1d37c98

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

leetcode刷题/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
- [No.8 字符串转换整数 (atoi)](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No8_my-atoi.md)
66
- [No.14 最长公共前缀](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No14_longest-common-prefix.md)
77
- [No.19 删除链表倒数第 n 个节点](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No19_remove-nth-from-end.md)
8+
- [No.20 有效的括号](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No20_is-valid.md)
89
- [No.21 合并两个有序链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No21_merge-two-lists.md)
910
- [No.26 从排序数组中删除重复项](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No26_remove-duplicates.md)
1011
- [No.36 有效的数独](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No36_isvalid-sudoku.md)
1112
- [No.38 报数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No38_count-and-say.md)
1213
- [No.48 旋转图像](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No48_rotate.md)
1314
- [No.66 加一](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No66_plus-one.md)
15+
- [No.70 爬楼梯](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No70_climb-stairs.md)
1416
- [No.122 买卖股票的最佳时机 II](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No122_max-profit.md)
1517
- [No.125 验证回文串](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No125_is-palindrome.md)
1618
- [No.136 只出现一次的数字](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No136_single-number.md)

leetcode刷题/note/No20_is-valid.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# No.20 有效的括号
2+
3+
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
4+
5+
有效字符串需满足:
6+
7+
1. 左括号必须用相同类型的右括号闭合。
8+
2. 左括号必须以正确的顺序闭合。
9+
10+
注意空字符串可被认为是有效字符串。
11+
12+
## 示例
13+
14+
示例 1:
15+
16+
```
17+
输入: "()"
18+
输出: true
19+
```
20+
21+
示例 2:
22+
```
23+
输入: "()[]{}"
24+
输出: true
25+
```
26+
27+
示例 3:
28+
```
29+
输入: "(]"
30+
输出: false
31+
```
32+
33+
示例 4:
34+
```
35+
输入: "([)]"
36+
输出: false
37+
```
38+
39+
示例 5:
40+
```
41+
输入: "{[]}"
42+
输出: true
43+
```
44+
45+
## 解题思路
46+
47+
使用堆栈的思想,如果遇到左括号全部进栈,遇到右括号,判断是否匹配,如果匹配则出栈,不匹配直接返回false。
48+
49+
最后如果栈的长度为 0, 则括号是匹配的。
50+
51+
代码如下:
52+
53+
```javascript
54+
/**
55+
* @param {string} s
56+
* @return {boolean}
57+
*/
58+
var isValid = function(s) {
59+
// 1、使用堆栈的思想
60+
let stack = [];
61+
let rule = {'}' : '{', ']' : '[', ')' : '('};
62+
63+
64+
65+
for (let char of s) {
66+
67+
// 遇到左括号全部进栈
68+
if (char == '[' || char == '{' || char == '(') {
69+
stack.push(char);
70+
} else {
71+
// 遇到右括号,判断是否匹配,如果匹配则出栈,不匹配直接返回false
72+
if (rule[char] == stack[stack.length - 1]) {
73+
stack.pop();
74+
} else {
75+
return false;
76+
}
77+
}
78+
}
79+
80+
return stack.length == 0;
81+
};
82+
83+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# No.70 爬楼梯
2+
3+
难度:`easy`
4+
5+
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
6+
7+
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
8+
9+
**注意:**给定 n 是一个正整数。
10+
11+
## 示例
12+
13+
示例 1:
14+
```
15+
输入: 2
16+
输出: 2
17+
解释: 有两种方法可以爬到楼顶。
18+
1. 1 阶 + 1 阶
19+
2. 2 阶
20+
```
21+
22+
23+
示例 2:
24+
```
25+
输入: 3
26+
输出: 3
27+
解释: 有三种方法可以爬到楼顶。
28+
1. 1 阶 + 1 阶 + 1 阶
29+
2. 1 阶 + 2 阶
30+
3. 2 阶 + 1 阶
31+
```
32+
33+
## 解题思路
34+
35+
根据示例,再向后推导几步,发现数列n = (n-1) + (n-2),即满足斐波那契数列。
36+
37+
由动态规划的思想可以知道,第 i 阶可以由以下两种方法得到:
38+
39+
- 在第 (i-1)(i−1) 阶后向上爬一阶。
40+
41+
- 在第 (i-2)(i−2) 阶后向上爬 22 阶。
42+
43+
代码如下:
44+
45+
```javascript
46+
// 递归超时,这里直接使用循环解决
47+
48+
/**
49+
* @param {number} n
50+
* @return {number}
51+
*/
52+
var climbStairs = function(n) {
53+
54+
// 满足斐波那契数列
55+
if (n == 1) {return 1;}
56+
if (n==2) {return 2;}
57+
58+
let total = 0;
59+
let [pre,preAndPre] = [2,1]
60+
61+
for (let i = 2; i < n; i++) {
62+
total = pre + preAndPre;
63+
preAndPre = pre;
64+
pre = total;
65+
}
66+
return total;
67+
68+
// 递归的思路
69+
// if (n == 1) {return 1;}
70+
// if (n==2) {return 2;}
71+
72+
// return climbStairs(n-1) + climbStairs(n-2);
73+
};
74+
```

0 commit comments

Comments
 (0)