Skip to content

Commit

Permalink
Merge pull request #6 from wangyuchaogeek/master
Browse files Browse the repository at this point in the history
ValidParentheses
  • Loading branch information
wangyuchaogeek authored Jun 16, 2019
2 parents 626af88 + 5325fbf commit a52689d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
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
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
| 参与成员 |
| ------ |
| [hongqima](https://github.com/hotchner) |
| [Wang-yuchao](https://github.com/Wang-yuchao) |
| [wangyuchaogeek](https://github.com/wangyuchaogeek) |
| [ZQMei](https://github.com/ZQMei) |
| [新哥_会飞的猴子](https://github.com/CoderLiuJixin) |

Expand All @@ -28,7 +28,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 a52689d

Please sign in to comment.