Skip to content

Commit

Permalink
ValidParentheses
Browse files Browse the repository at this point in the history
ValidParentheses
  • Loading branch information
wangyuchaogeek committed Jun 14, 2019
1 parent 25512e2 commit d22c28d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
29 changes: 29 additions & 0 deletions 0020-有效的括号/Swift/ValidParentheses.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// LongestCommonPrefix.swift
// Created by 王玉朝 on 2019/6/14.
// Copyright © 2019 王玉朝. All rights reserved.

class ValidParentheses {
func isValid(_ s: String) -> Bool {
var stack = [Character]()

for char in s {
if char == "(" || char == "[" || char == "{" {
stack.append(char)
} else if char == ")" {
guard stack.count != 0 && stack.removeLast() == "(" else {
return false
}
} else if char == "]" {
guard stack.count != 0 && stack.removeLast() == "[" else {
return false
}
} else if char == "}" {
guard stack.count != 0 && stack.removeLast() == "{" else {
return false
}
}
}

return stack.isEmpty
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| [hongqima](https://github.com/hotchner) |
| [Wang-yuchao](https://github.com/Wang-yuchao) |
| [ZQMei](https://github.com/ZQMei) |
| [wangleiTT](https://github.com/wangleiTT) |
| [新哥_会飞的猴子](https://github.com/CoderLiuJixin) |

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

0 comments on commit d22c28d

Please sign in to comment.