1047.删除字符串中的所有相邻重复项:优化Swift版本实现

This commit is contained in:
bqlin
2021-12-20 20:13:42 +08:00
committed by Bq
parent 08e226bb00
commit 343ddb37f9

View File

@ -322,14 +322,12 @@ char * removeDuplicates(char * s){
Swift
```swift
func removeDuplicates(_ s: String) -> String {
let array = Array(s)
var stack = [Character]()
for c in array {
let last: Character? = stack.last
if stack.isEmpty || last != c {
stack.append(c)
} else {
for c in s {
if stack.last == c {
stack.removeLast()
} else {
stack.append(c)
}
}
return String(stack)