题解 | #螺旋矩阵#

螺旋矩阵

http://www.nowcoder.com/practice/7edf70f2d29c4b599693dc3aaeea1d31

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
全部评论

相关推荐

缒梦&独舞:这家公司是这样的,去年给我实习offer了,不过也是面着玩儿的,他周六还要去做公益志愿活动
点赞 评论 收藏
分享
每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务