Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 98bdccb

Browse files
committed
添加 1047.删除字符串中的所有相邻重复项.md Scala版本
1 parent 61f5d92 commit 98bdccb

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,27 @@ func removeDuplicates(_ s: String) -> String {
374374
return String(stack)
375375
}
376376
```
377-
377+
Scala:
378+
```scala
379+
object Solution {
380+
import scala.collection.mutable
381+
def removeDuplicates(s: String): String = {
382+
var stack = mutable.Stack[Int]()
383+
var str = "" // 保存最终结果
384+
for (i <- s.indices) {
385+
var tmp = s(i)
386+
// 如果栈非空并且栈顶元素等于当前字符,那么删掉栈顶和字符串最后一个元素
387+
if (!stack.isEmpty && tmp == stack.head) {
388+
str = str.take(str.length - 1)
389+
stack.pop()
390+
} else {
391+
stack.push(tmp)
392+
str += tmp
393+
}
394+
}
395+
str
396+
}
397+
}
398+
```
378399
-----------------------
379400
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /