Skip to content

Commit 3abb6e1

Browse files
Merge pull request apachecn#25 from jiangzhonglian/master
添加 18 + 20题 解决方案
2 parents e977681 + 55b1fa9 commit 3abb6e1

File tree

4 files changed

+132
-10
lines changed

4 files changed

+132
-10
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
88
* **LeetCode 刷题群 | ApacheCN【812791932】<a target="_blank" href="//shang.qq.com/wpa/qunwpa?idkey=1d390faa76fe789a0068dadae4ab9b0f0fc7997c38f216e9a30172866163a49d"><img border="0" src="/images/MainPage/ApacheCN-group.png" alt="LeetCode 刷题 | ApacheCN " title="LeetCode 刷题 | ApacheCN "></a>**
99
* [<font color=green>ApacheCN repo 地址</font>](https://github.com/apachecn/leetcode): https://github.com/apachecn/leetcode
10-
* **<font color=green>ApacheCN 网友捐赠页面</font>:http://www.apachecn.org/organization/664.html**
1110

1211
## LeetCode 习题集合
1312

@@ -74,6 +73,13 @@ ApacheCN 纯粹出于学习目的与个人兴趣整理的内容。
7473

7574
整理内容只供学习研究参考之用。ApacheCN 保留对此版本内容的署名权及其它相关权利。
7675

76+
77+
## 赞助我们
78+
79+
* **<font color=green>ApacheCN 网友捐赠页面</font>:http://www.apachecn.org/organization/664.html**
80+
81+
![](http://www.apachecn.org/wp-content/uploads/2018/02/%E6%94%AF%E4%BB%98-%E5%BE%AE%E4%BF%A1%E5%92%8C%E6%94%AF%E4%BB%98%E5%AE%9D-1024x591.png)
82+
7783
## [ApacheCN 组织资源](http://www.apachecn.org/)
7884

7985
> [kaggle: 机器学习竞赛](https://github.com/apachecn/kaggle)

docs/Leetcode_Solutions/001._two_sum.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
所以返回 [0, 1]
1919
```
2020

21-
## 解决方案
21+
## 解题方案
2222

2323
> 思路 1
2424

docs/Leetcode_Solutions/020._valid_parentheses.md

+79-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,50 @@
1-
### 20. Valid Parentheses
1+
# 20. Valid Parentheses 有效的括号
22

3-
题目:
3+
**<font color=red>难度: Easy</font>**
44

5-
<https://leetcode.com/problems/valid-parentheses/>
5+
## 刷题内容
66

7+
> 原题连接
78
8-
难度:
9+
* https://leetcode.com/problems/valid-parentheses
10+
* https://leetcode-cn.com/problems/valid-parentheses/description
911

10-
Easy
12+
> 内容描述
1113
14+
```
15+
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
16+
17+
有效字符串需满足:
18+
19+
左括号必须用相同类型的右括号闭合。
20+
左括号必须以正确的顺序闭合。
21+
注意空字符串可被认为是有效字符串。
22+
23+
示例 1:
24+
25+
输入: "()"
26+
输出: true
27+
示例 2:
28+
29+
输入: "()[]{}"
30+
输出: true
31+
示例 3:
32+
33+
输入: "(]"
34+
输出: false
35+
示例 4:
1236
13-
### 思路:
37+
输入: "([)]"
38+
输出: false
39+
示例 5:
40+
41+
输入: "{[]}"
42+
输出: true
43+
```
44+
45+
## 解题方案
46+
47+
> 思路 1
1448
1549
因为一共只有三种状况"(" -> ")", "[" -> "]", "{" -> "}".
1650

@@ -22,8 +56,6 @@ Easy
2256
- 出stack时是否对应
2357
- 最终stack是否为空
2458

25-
26-
2759
```python
2860
class Solution(object):
2961
def isValid(self, s):
@@ -50,3 +82,42 @@ class Solution(object):
5082
return stack == []
5183
```
5284

85+
> 思路 2
86+
87+
* 扩展性和可理解性强
88+
89+
```python
90+
class Solution:
91+
def isValid(self, s):
92+
"""
93+
:type s: str
94+
:rtype: bool
95+
"""
96+
if len(s) % 2 == 1:
97+
return False
98+
99+
index = 0
100+
stack = [i for i in s]
101+
map1 = {"(": ")", "[": "]", "{": "}"}
102+
103+
while len(stack) > 0:
104+
# 判断索引是否超过边界
105+
if index >= len(stack)-1:
106+
return False
107+
108+
b = stack[index]
109+
e = stack[index+1]
110+
111+
if b not in map1.keys():
112+
return False
113+
elif e in map1.keys():
114+
index += 1
115+
elif map1[b] == e:
116+
stack.pop(index+1)
117+
stack.pop(index)
118+
index = 0 if index-1<0 else index-1
119+
else:
120+
return False
121+
122+
return stack == []
123+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 181. duplicate-emails 查找重复的电子邮箱
2+
3+
**<font color=red>难度: Easy</font>**
4+
5+
## 刷题内容
6+
7+
> 原题连接
8+
9+
* https://leetcode.com/problems/duplicate-emails
10+
* https://leetcode-cn.com/problems/duplicate-emails/description
11+
12+
> 内容描述
13+
14+
```
15+
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。
16+
17+
示例:
18+
+----+---------+
19+
| Id | Email |
20+
+----+---------+
21+
22+
23+
24+
+----+---------+
25+
26+
根据以上输入,你的查询应返回以下结果:
27+
+---------+
28+
| Email |
29+
+---------+
30+
31+
+---------+
32+
33+
说明:所有电子邮箱都是小写字母。
34+
```
35+
36+
## 解决方案
37+
38+
> 思路 1
39+
40+
```sql
41+
select Email
42+
from Person
43+
group by Email
44+
having count(1)>1
45+
```

0 commit comments

Comments
 (0)