添加1047.删除字符串中的所有相邻重复项 Ruby实现

This commit is contained in:
han
2023-08-10 10:21:29 +08:00
parent bf51d4730d
commit 7ae878753b

View File

@ -475,6 +475,26 @@ impl Solution {
}
```
### Ruby
```ruby
def remove_duplicates(s)
#数组模拟栈
stack = []
s.each_char do |chr|
if stack.empty?
stack.push chr
else
head = stack.pop
#重新进栈
stack.push head, chr if head != chr
end
end
return stack.join
end
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>