return [] if matrix.length == 0 || matrix[0].length == 0
ls = [] #返回的数组
up, down = 0, matrix.length-1
left, right = 0, matrix[0].length-1
while ( up <= matrix.length/2 && left <= matrix[0].length/2) do
#向右移动
(left..right).each{|d|
ls << matrix[up][d]
}
#向下移动
(up+1..down).each{|d|
ls << matrix[d][right]
}
#向左移动
tps = []
(left..right-1).each{|d|
tps << matrix[down][d]
}
ls.concat(tps.reverse)
#向上移动
tps = []
(up+1..down-1).each{|d|
tps << matrix[d][left]
}
ls.concat(tps.reverse)
up += 1
down -= 1
left += 1
right -= 1
end
ls.uniq


京公网安备 11010502036488号