Given aNXN matrix, starting from the upper right corner of the matrix start printingvalues in a counter-clockwise fashion.

E.g.: Consider N = 4

Matrix= {a, b, c, d,

e, f, g, h,

i, j, k, l,

m, n, o, p}

Your function should output: dcbaeimnoplhgfjk

分成四段 然后index依次减一 一共执行n/2次

对于n为奇数 最后再加入一个中间值

def spiral(a)
ans, n = [], a.length
n/2.times do |k|
(k...n-k-1).each {|i| ans << a[k][i]}
(k...n-k-1).each {|i| ans << a[i][n-k-1]}
(k...n-k-1).each {|i| ans << a[n-k-1][-i-1]}
(k...n-k-1).each {|i| ans << a[-i-1][k]}
end
ans << a[n/2][n/2] if n%2 == 1
ans
end

Epic - Spiral Matrix的更多相关文章

  1. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  2. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  3. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  4. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  5. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  6. 59. Spiral Matrix && Spiral Matrix II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  7. LeetCode:Spiral Matrix I II

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  8. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  9. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

随机推荐

  1. Oracle ->> Oracle下生成序列的方法

    用hierachical query,即connect by配合dual表生成序列,mod这个是取余函数,生成group factor.最后面的connect by rownum <= 100可 ...

  2. 如何通过图片在 HTTPS 网站中获取 HTTP 接口数据

    <script> (function() { var Decode=function(b){var e;e=[];var a=b.width,c=b.height,d=document.c ...

  3. ASP.NET26个性能优化方法

    1.数据库访问性能优化 (1)数据库的连接和关闭 访问数据库资源需要创建连接.打开连接和关闭连接几个操作.这些过程需要多次与数据库交换信息以通过身份验证,比较耗费服务器资源. ASP.NET中提供了连 ...

  4. c 建立工程 常见错误及心得总结

    1. 调用函数里面的 调用子函数,要是出现已定义错误,则要看看,是不是主函数的 头文件写成.c啦,因为在.h里面也定义可一次,要是写.c出现两次 错误提示:UrlCheck.obj : error L ...

  5. YTU 2619: B 友元类-计算两点间距离

    2619: B 友元类-计算两点间距离 时间限制: 1 Sec  内存限制: 128 MB 提交: 469  解决: 252 题目描述 类Distance定义为类Point的友元类来实现计算两点之间距 ...

  6. sysfs - 用于导出内核对象(kobject)的文件系统

    sysfs - _The_ filesystem for exporting kernel objects.sysfs - 用于导出内核对象(kobject)的文件系统Patrick Mochel & ...

  7. C# 时间戳和时间的相互转换

    时间戳定义为从格林威治时间 1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数. C#格式时间转时间戳Timestamp private in ...

  8. 我的Linux书架

    原文地址www.cnblogs.com/wwang/archive/2011/01/27/1944406.html 工作几年来,一直从事Linux内核驱动方面的开发.从接触Linux到现在,读过不少L ...

  9. Gson解析POJO类中的泛型参数

    在开发Android与API交互的时候,使用Json格式传输,遇到了这样一个情况,返回数据格式POJO类如下: public class ApiResult<T> { private in ...

  10. js之客户端检测

    1 能力检测判断是否有某个属性或方法,例:在低版本中的opera浏览器中,window下有个opera属性 if (window.opera) { alert("opera"); ...