Skip to content

Commit a52689d

Browse files
Merge pull request #6 from wangyuchaogeek/master
ValidParentheses
2 parents 626af88 + 5325fbf commit a52689d

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// LongestCommonPrefix.swift
2+
// Created by 王玉朝 on 2019/6/14.
3+
// Copyright © 2019 王玉朝. All rights reserved.
4+
5+
class ValidParentheses {
6+
func isValid(_ s: String) -> Bool {
7+
var stack = [Character]()
8+
9+
for char in s {
10+
if char == "(" || char == "[" || char == "{" {
11+
stack.append(char)
12+
} else if char == ")" {
13+
guard stack.count != 0 && stack.removeLast() == "(" else {
14+
return false
15+
}
16+
} else if char == "]" {
17+
guard stack.count != 0 && stack.removeLast() == "[" else {
18+
return false
19+
}
20+
} else if char == "}" {
21+
guard stack.count != 0 && stack.removeLast() == "{" else {
22+
return false
23+
}
24+
}
25+
}
26+
27+
return stack.isEmpty
28+
}
29+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
| 参与成员 |
44
| ------ |
55
| [hongqima](https://github.com/hotchner) |
6-
| [Wang-yuchao](https://github.com/Wang-yuchao) |
6+
| [wangyuchaogeek](https://github.com/wangyuchaogeek) |
77
| [ZQMei](https://github.com/ZQMei) |
88
| [新哥_会飞的猴子](https://github.com/CoderLiuJixin) |
99

@@ -28,7 +28,7 @@
2828
| 17 | [电话号码的字母组合](https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number) | 中等 | | | | | |
2929
| 18 | [四数之和](https://leetcode-cn.com/problems/4sum) | 中等 | | | | | |
3030
| 19 | [删除链表的倒数第N个节点](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list) | 中等 | | | | | |
31-
| 20 | [有效的括号](https://leetcode-cn.com/problems/valid-parentheses) | 简单 | | | | | |
31+
| 20 | [有效的括号](https://leetcode-cn.com/problems/valid-parentheses) | 简单 |[Swift](0020-有效的括号/Swift) | | | | |
3232
| 21 | [合并两个有序链表](https://leetcode-cn.com/problems/merge-two-sorted-lists) | 简单 | | | | | |
3333
| 22 | [括号生成](https://leetcode-cn.com/problems/generate-parentheses) | 中等 | | | | | |
3434
| 23 | [合并K个排序链表](https://leetcode-cn.com/problems/merge-k-sorted-lists) | 困难 | | | | | |

0 commit comments

Comments
 (0)