Merge pull request #2229 from niuli1991/master-1047

添加1047.删除字符串中的所有相邻重复项 Ruby实现
This commit is contained in:
程序员Carl
2023-08-21 14:21:35 +08:00
committed by GitHub

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"/>