[Swift]LeetCode1047.刪除字符串中的全部相鄰重複項 | Remove All Adjacent Duplicates In String

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ekhgwixw-mc.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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

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

Example 1:app

Input: "abbaca"
Output: "ca" Explanation: For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move.  The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".

Note:this

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

給出由小寫字母組成的字符串 S,重複項刪除操做會選擇兩個相鄰且相同的字母,並刪除它們。spa

在 S 上反覆執行重複項刪除操做,直到沒法繼續刪除。code

在完成全部重複項刪除操做後返回最終的字符串。答案保證惟一。htm

示例:blog

輸入:"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 }
相關文章
相關標籤/搜索