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. 如何在Java 8中愉快地处理日期和时间

    Java 8新增了LocalDate和LocalTime接口,为什么要搞一套全新的处理日期和时间的API?因为旧的java.util.Date实在是太难用了. java.util.Date月份从0开始 ...

  2. Hadoop HDFS文件常用操作及注意事项

    Hadoop HDFS文件常用操作及注意事项 1.Copy a file from the local file system to HDFS The srcFile variable needs t ...

  3. 基于EntityFramework的权限的配置和验证

    1.   概要 本文主要介绍公司现有系统框架的权限体系,和一些待扩展功能的说明.目前该权限体系基于角色构建(RBAC),原则上,系统中不允许出现对用户.组织等其他对象指派权限的情况. 2.   权限分 ...

  4. Android: 启动init.rc 中service的权限问题【转】

    转自:http://www.linuxidc.com/Linux/2011-04/35014.htm 通过property_set("ctl.start", service_xx) ...

  5. Android里的多线程知识点

    1.Thread类与Runnable接口 子类继承Thread类实现跑自己逻辑的run方法,在调用Thread类的start方法后,会自动调用run方法,该对象只可以调用一次start方法,即Thre ...

  6. centos6.5 中文

    之前在网上查了不少资料,很多网友在网上都说,在shell命令下输入:     # vi  /etc/sysconfig/i18n     然后修改LANG="en_US.UTF-8" ...

  7. 《OD学hadoop》第一周0625 LINUX作业一:Linux系统基本命令(一)

    1. 1) vim /etc/udev/rules.d/-persistent-net.rules vi /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE= ...

  8. Headfirst JSP 01 (概述)

    HTTP 协议 http 是tcp/ip上层协议, 如果你对这些网络协议还不是太熟悉, 下面提供一个非常简单的解释, tcp负责确保从一个网络节点向另一个网络节点发送文件能作为一个完整的文件到达目的地 ...

  9. Android实现分享内容到微信朋友圈

    原文地址:http://yanwushu.sinaapp.com/android_wechat_share/ 由于需求,要实现在应用中实现分享文字+图片到微信朋友圈.在网上找了一些资料,总结如下: 思 ...

  10. hibernate lazy=false annotation设置

    工程报错如下: org.hibernate.LazyInitializationException: could not initialize proxy - no Session 解决方法: 在类的 ...