Skip to content

Commit d22c28d

Browse files
ValidParentheses
ValidParentheses
1 parent 25512e2 commit d22c28d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| [hongqima](https://github.com/hotchner) |
66
| [Wang-yuchao](https://github.com/Wang-yuchao) |
77
| [ZQMei](https://github.com/ZQMei) |
8+
| [wangleiTT](https://github.com/wangleiTT) |
89
| [新哥_会飞的猴子](https://github.com/CoderLiuJixin) |
910

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

0 commit comments

Comments
 (0)