★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9697906.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:

Input: "()"
Output: true

Example 2:

Input: "()[]{}"
Output: true

Example 3:

Input: "(]"
Output: false

Example 4:

Input: "([)]"
Output: false

Example 5:

Input: "{[]}"
Output: true

给定一个只包括 '('')''{''}''['']' 的字符串,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

示例 1:

输入: "()"
输出: true

示例 2:

输入: "()[]{}"
输出: true

示例 3:

输入: "(]"
输出: false

示例 4:

输入: "([)]"
输出: false

示例 5:

输入: "{[]}"
输出: true

 1 class Solution {2     func isValid(_ s: String) -> Bool {3         //字符串为空返回true4         if s.isEmpty5         {6             return true7         }8         else9         {
10             //或字符串的字符个数为奇数个直接排除
11             if s.count%2==1
12             {
13                 return false
14             }
15         }
16         //创建字典对照表
17         var map:[Character:Character]=[")":"(","}":"{","]":"["]
18         //偶数符串若第一个字符就是右边的符号则直接排除
19         if  map[s[s.startIndex]] != nil
20         {
21             return false
22         }
23         //创建字符串堆栈
24         var stackOfString=Stack<Character>()
25         //遍历字符串
26         for char in s
27         {
28             //为右侧符号,且查询字典对应堆栈中最后一个元素
29             if map[char] != nil && map[char]==stackOfString.GetLastElement()
30             {
31                 //出栈
32                 stackOfString.pop()
33             }
34             else
35             {
36                 //入栈
37                 stackOfString.push(char)
38             }
39         }
40         return stackOfString.count()==0
41     }
42     //堆栈的泛型通用版本
43     struct Stack<Element> {
44         var items = [Element]()
45         //入栈
46         //mutating 关键字修饰方法是为了能在该方法中修改 struct 或是 enum 的变量
47         mutating func push(_ item: Element) {
48             items.append(item)
49         }
50         //出栈
51         mutating func pop() -> Element {
52             return items.removeLast()
53         }
54         //返回堆栈中的元素个数
55         mutating func count()->Int
56         {
57             return items.count
58         }
59         //获取最后一个元素
60         mutating func GetLastElement()->Element
61         {
62             return items[items.count-1]
63         }
64     }
65 }


高效率版

 1 class Solution {2     func isValid(_ s: String) -> Bool {3         var opened = ""4         for char in s { 5             if char == "(" {6                 opened += ")" 7             } else if char == "{" {8                 opened += "}"9             } else if char == "[" {
10                 opened += "]"
11             } else {
12                 if let prevVal = opened.last, prevVal == char {
13                     opened.removeLast()
14                 } else {
15                     return false
16                 }
17             }
18         }
19         return opened.isEmpty
20     }
21 }

转载于:https://www.cnblogs.com/strengthen/p/9697906.html

[Swift]LeetCode20. 有效的括号 | Valid Parentheses相关推荐

  1. LeetCode 20. 有效的括号(Valid Parentheses)

    栈思想 关于有效括号表达式的一个有趣属性是有效表达式的子表达式也应该是有效表达式. 整个表达式是有效的,而它的子表达式本身也是有效的.这为问题提供了一种递归结构.(栈思想) 从整体表达式中一次删除一个 ...

  2. LeetCode 之 JavaScript 解答第20题 —— 有效的括号(Valid Parentheses)

    Time:2019/4/11 Title: Valid Parentheses Difficulty: Easy Author: 小鹿 题目:Valid Parentheses Given a str ...

  3. 最长有效括号子串长度 c语言,LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)...

    题目描述: Given a string containing just the characters'(' and')', find the length of the longest valid ...

  4. LeetCode Longest Valid Parentheses

    原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...

  5. LeetCode 32. Longest Valid Parentheses

    问题链接 LeetCode 32. Longest Valid Parentheses 题目解析 给出只包含左右括号的字符串,返回最长的括号匹配字符串长度. 解题思路 括号匹配问题一般借助 栈,便于理 ...

  6. LeetCode算法入门- Longest Valid Parentheses -day12

    LeetCode算法入门- Longest Valid Parentheses -day12 Given a string containing just the characters '(' and ...

  7. LeetCode算法入门- Valid Parentheses -day11

    LeetCode算法入门- Valid Parentheses -day11 题目描述: Given a string containing just the characters '(', ')', ...

  8. Longest Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  9. Leetcode: Longest Valid Parentheses

    Question Given a string containing just the characters '(' and ')', find the length of the longest v ...

  10. Java题目详解——LeetCode20.有效的括号

    目录 题目链接:LeetCode20.有效的括号 一.题目要求 二.解题思路 三.具体代码 四.运行截图 题目链接:LeetCode20.有效的括号 一.题目要求 给定一个只包括 '(',')','{ ...

最新文章

  1. 异步消息队列zeromq实现服务器间高性能通信
  2. linux shell eval 命令 字符串作为命令执行
  3. 优化 bulk insert
  4. hexo博客生成博文,当生成的文章数量超过1000时,耗尽所有内存资源后出现out of memory
  5. sparse double型矩阵转为full矩阵
  6. Java黑皮书课后题第4章:*4.13(判断元音还是辅音)编写程序,提示用户输入一个字母,判断该字母是元音还是辅音。对于非字母的输入,提示非法输入
  7. 新发现一款监控Linux集群sinfo
  8. 搭建实用深度学习环境(Ubuntu16.10+Theano0.8.2+Tensorflow0.11.0rc1+Keras1.1.0)
  9. Java分布式唯一ID生成方案——比UUID效率更高的生成id工具类
  10. 来鹅厂干大事!腾讯广告技术类岗位高能来袭~
  11. Conda 下 安装 Allennlp
  12. Django深入模板引擎
  13. MDK5软件入门之新建工程项目模板
  14. 第二篇:Spring Cloud Eureka 服务注册+发现
  15. 目前见到的最傻瓜全面的STRUTS入门教程^_^
  16. Cocos2d之Box2d基础知识
  17. Arduino智能浇灌系统
  18. 计算机中 8位无符号数,8位无符号数乘法运算HDL设计实例 - 全文
  19. thymleaf 使用三目运算多个条件判断的写法
  20. 民族企业家周景川:凡事勤则易,凡事惰则难

热门文章

  1. Spring源码之ApplicationContext(二)准备工作
  2. 【渝粤教育】电大中专电子商务网站建设与维护 (17)作业 题库
  3. Caffe 数据结构
  4. PyTorch并行与分布式(四)Distributed Data Papallel
  5. 循环神经网络系列(一) RNN、双向RNN、深度RNN
  6. linux大鹏命令百篇
  7. linux系统Vsftpd搭建FTP
  8. 知识、经验的漏洞还有很多很多
  9. [转]Java计时器Timer 使用
  10. require.js使用教程