From d22c28d57d49c91e2288b529ffb17a1c0bed5edc Mon Sep 17 00:00:00 2001 From: wang-yuchao Date: Fri, 14 Jun 2019 17:25:15 +0800 Subject: [PATCH] ValidParentheses ValidParentheses --- .../Swift/ValidParentheses.swift" | 29 +++++++++++++++++++ README.md | 3 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100755 "0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/Swift/ValidParentheses.swift" diff --git "a/0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/Swift/ValidParentheses.swift" "b/0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/Swift/ValidParentheses.swift" new file mode 100755 index 0000000..ade63c0 --- /dev/null +++ "b/0020-\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267/Swift/ValidParentheses.swift" @@ -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 + } +} diff --git a/README.md b/README.md index 03de230..6ec5e35 100644 --- a/README.md +++ b/README.md @@ -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 | @@ -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) | 困难 | | | | | |