Epic - Spiral Matrix
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的更多相关文章
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 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 ...
- 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 ...
- 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 ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
随机推荐
- 解决Android开发中,ActiveAndroid和Gson同时使用,对象序列化失败的问题
ActiveAndroid是安卓开发常用的ORM框架. Gson则是Google提供的轻量级序列化框架,非常适合Android开发使用. 但这两者同时使用,会产生序列化失败的问题.你通常会收到如下信息 ...
- LA 4255 Guess
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- httpClient.execute之后一直等待
可能的原因就是之前执行过一次execute,但是没有释放资源. hrp = httpClient.execute(req); //这句释放资源 hrp.getEntity().consumeConte ...
- oraclede chuangjian yu dajian(zhuan)
http://wenku.baidu.com/link?url=pIKLZJ4sAurjNGjwgChqjRMhCXfn77qy1K_EW3nlGn4eN4roDN8mhSG0GakYbrTBcsD4 ...
- BZOJ 2429 聪明的猴子
kruskal. #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...
- nanakon
1.安装python pip3 install tornado pip3 install pymysql pip3 install qiniu pip3 install pillow 2.安装mysq ...
- 20160205.CCPP体系详解(0015天)
程序片段(01):01.杨辉三角.c 内容概要:杨辉三角 #include <stdio.h> #include <stdlib.h> #define N 10 //01.杨辉 ...
- Filling a Path 模式
Filling a Path When you fill the current path, Quartz acts as if each subpath contained in the path ...
- pg psql命令
linux下使用psql命令操作数据库 下面主要用到了insert into ,pg_dump , pg_restore 命令 按步骤走 su postgres ...
- pg 匹配中文字符
用到了正则表达式: 字段 ~'[\u4E00-\u9FA5]+$'; 注意:此表达式可能还不能取到最全的值.