Merge branch 'master' into 1047

This commit is contained in:
程序员Carl
2022-07-01 09:43:42 +08:00
committed by GitHub
179 changed files with 5178 additions and 415 deletions

View File

@@ -11,7 +11,7 @@
# 1047. 删除字符串中的所有相邻重复项
[力扣题目链接](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/)
[力扣题目链接](https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/)
给出由小写字母组成的字符串 S重复项删除操作会选择两个相邻且相同的字母并删除它们。
@@ -375,6 +375,7 @@ func removeDuplicates(_ s: String) -> String {
}
```
PHP:
```php
class Solution {
@@ -400,5 +401,29 @@ class Solution {
}
```
Scala:
```scala
object Solution {
import scala.collection.mutable
def removeDuplicates(s: String): String = {
var stack = mutable.Stack[Int]()
var str = "" // 保存最终结果
for (i <- s.indices) {
var tmp = s(i)
// 如果栈非空并且栈顶元素等于当前字符,那么删掉栈顶和字符串最后一个元素
if (!stack.isEmpty && tmp == stack.head) {
str = str.take(str.length - 1)
stack.pop()
} else {
stack.push(tmp)
str += tmp
}
}
str
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>