Merge branch 'master' into master
This commit is contained in:
@@ -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 {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
C#:
|
||||
```csharp
|
||||
public string RemoveDuplicates(string s) {
|
||||
@@ -392,5 +393,56 @@ public string RemoveDuplicates(string s) {
|
||||
return res.ToString();
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
function removeDuplicates($s) {
|
||||
$stack = new SplStack();
|
||||
for($i=0;$i<strlen($s);$i++){
|
||||
if($stack->isEmpty() || $s[$i] != $stack->top()){
|
||||
$stack->push($s[$i]);
|
||||
}else{
|
||||
$stack->pop();
|
||||
}
|
||||
}
|
||||
|
||||
$result = "";
|
||||
while(!$stack->isEmpty()){
|
||||
$result.= $stack->top();
|
||||
$stack->pop();
|
||||
}
|
||||
|
||||
// 此时字符串需要反转一下
|
||||
return strrev($result);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
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>
|
||||
|
||||
Reference in New Issue
Block a user