• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

[Swift]LeetCode1047.删除字符串中的所有相邻重复项|RemoveAllAdjacentDuplicatesInSt ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

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

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

Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.

We repeatedly make duplicate removals on S until we no longer can.

Return the final string after all such duplicate removals have been made.  It is guaranteed the answer is unique.

Example 1:

Input: 

Note:

  1. 1 <= S.length <= 20000
  2. S consists only of English lowercase letters.

给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。

在 S 上反复执行重复项删除操作,直到无法继续删除。

在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。

示例:

输入:"abbaca"
输出:"ca"
解释:
例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。

提示:

  1. 1 <= S.length <= 20000
  2. S 仅由小写英文字母组成。

Runtime: 132 ms
Memory Usage: 21.4 MB
 1 class Solution {
 2     func removeDuplicates(_ S: String) -> String {
 3         var res:String = String()
 4         for c in S
 5         {
 6             if res.isEmpty || res.last! != c
 7             {
 8                 res.append(c)
 9             }
10             else
11             {
12                 res.popLast()
13             }
14         }
15         return res        
16     }
17 }

132ms

 1 class Solution {
 2   func removeDuplicates(_ S: String) -> String {
 3     var s = S
 4     var i = s.startIndex
 5     while !s.isEmpty && s.index(after: i) != s.endIndex {
 6       if s[i] == s[s.index(after: i)] {
 7         s.remove(at: i)
 8         s.remove(at: i)
 9         if i != s.startIndex {
10           i = s.index(before: i)
11         }
12         continue
13       }
14       i = s.index(after: i)
15     }
16     return s
17   }
18 }

144ms

 1 class Solution {
 2     func removeDuplicates(_ S: String) -> String
 3     {
 4         var list: [Character] = []
 5         for c in S
 6         {
 7             if let last = list.last
 8             {
 9                 if last == c {
10                     list.removeLast()
11                 }
12                 else { list.append(c) }
13             }
14             else {
15                 list.append(c)
16             }
17         }
18         return String(list)
19     }
20 }

196ms

 1 class Solution {
 2     func removeDuplicates(_ S: String) -> String {
 3         var stack = [Character]()
 4         
 5         for char in S {
 6             if stack.last == char {
 7                 stack.removeLast()
 8             } else {
 9                 stack.append(char)
10             }
11         }
12         
13         return String(stack)
14     }
15 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swift也开源了.发布时间:2022-07-13
下一篇:
[Swift]LeetCode235.二叉搜索树的最近公共祖先|LowestCommonAncestorofaBinarySearchT ...发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap