mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
添加1047.删除字符串中的所有相邻重复项 Ruby实现
This commit is contained in:
@ -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"/>
|
||||
|
Reference in New Issue
Block a user