我需要编写一个程序,该程序在给定一个整数的情况下,查找重复数字的字符串,并将它们作为数组返回.例如,给定1234555567899944
,程序需要返回[5555, 999, 44]
.
以下是我目前掌握的情况:
a = 1234555567899944
arr = a.to_s.split("")
result = []
arr.each_with_index do |x,y|
if arr[y] == arr[y+1] || arr[y] == arr[y-1]
result << x
end
end
p result.join().split()
有没有更好的办法?
我怎么才能得到[5555, 999, 44]
分而不是["555599944"]
分呢?