Merge pull request #1076 from bqlin/master

优化版本,优化 Swift 版本实现
This commit is contained in:
程序员Carl
2022-02-19 12:15:35 +08:00
committed by GitHub
4 changed files with 42 additions and 27 deletions

View File

@@ -346,14 +346,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)