Skip to content

Commit 9fd8ea5

Browse files
committed
添加 1047. 删除字符串中的所有相邻重复项 Swift版本
1 parent c9d8cff commit 9fd8ea5

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

problems/1047.删除字符串中的所有相邻重复项.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,22 @@ char * removeDuplicates(char * s){
319319
}
320320
```
321321

322+
Swift:
323+
```swift
324+
func removeDuplicates(_ s: String) -> String {
325+
let array = Array(s)
326+
var stack = [Character]()
327+
for c in array {
328+
let last: Character? = stack.last
329+
if stack.isEmpty || last != c {
330+
stack.append(c)
331+
} else {
332+
stack.removeLast()
333+
}
334+
}
335+
return String(stack)
336+
}
337+
```
322338

323339
-----------------------
324340
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
 (0)