新手问题 把数组分组

chyanog · October 21, 2012 · Last by chyanog replied at October 22, 2012 · 4289 hits

example:

[1,1,1,2,2,3,1,1]

---->

[[1,1,1],[2,2],[3],[1,1]]

顺序不能变

res, mid = [], [] arr = [1,1,1,2,2,3,1,1] arr.each{|i| unless mid.empty? || i == mid.last; res << mid; mid = []; end; mid << i}; res << mid 这样 OK 么?

a = [1,1,1,2,2,3,1,1]
a.chunk{|x|x}.map &:last

都不错啊

You need to Sign in before reply, if you don't have an account, please Sign up first.