mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #1410 from fmtvar/1047
添加(1047.删除字符串中的所有相邻重复项.md):php版本
This commit is contained in:
@ -374,6 +374,34 @@ func removeDuplicates(_ s: String) -> String {
|
||||
return String(stack)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
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 {
|
||||
@ -396,5 +424,6 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<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