,敢教日月换新天。为有牺牲多壮志

[Swift]LeetCode20. 有效的括号 | Valid Parentheses

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

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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         //字符串为空返回true
 4         if s.isEmpty
 5         {
 6             return true
 7         }
 8         else
 9         {
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 }
复制代码
posted @   为敢技术  阅读(477)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°