有这样一个题目:

Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.

array = [[1,2,3],
[4,5,6],
[7,8,9]]
snail(array) #=> [1,2,3,6,9,8,7,4,5]

For better understanding, please follow the numbers of the next array consecutively:

array = [[1,2,3],
[8,9,4],
[7,6,5]]
snail(array) #=> [1,2,3,4,5,6,7,8,9]

This image will illustrate things more clearly:

NOTE: The idea is not sort the elements from the lowest value to the highest; the idea is to traverse the 2-d array in a clockwise snailshell pattern.

NOTE 2: The 0x0 (empty matrix) is represented as [[]]

题目意思是,给出一个N*N的二维数组,要求输出一个顺时针的一维数组,0*0的和1*1的直接原样一维数组返回就行了。

下面是我的解法:

snail = function (array) {
// enjoy
let len = array.length;
console.log(len);
if (array.length == 1) {
return array[0];
} else {
let i = 0, j = len - 1;
let rowStart = 1, rowEnd = len - 1, colStart = 0, colEnd = len - 2;
let direction = 'down';
for (let index = 0; index < len * (len - 1); index++) {
console.log(direction, 'direction');
switch (direction) {
case 'down':
i++;
if (i >= rowEnd) {
i = rowEnd;
rowEnd--;
console.log('down', i, rowEnd);
direction = 'left';
}
break;
case 'left':
j--;
if (j <= colStart) {
j = colStart;
colStart++;
console.log('left');
direction = 'up';
}
break;
case 'up':
i--;
if (i <= rowStart) {
i = rowStart;
rowStart++;
console.log('up');
direction = 'right';
}
break;
case 'right':
j++;
if (j >= colEnd) {
j = colEnd;
colEnd--;
console.log('right');
direction = 'down';
}
break;
}
console.log(array[i][j], i, j);
array[0].push(array[i][j]);
} }
return array[0];
}

  虽然感觉啰嗦,但是勉强实现了。

  下面看看得分最高的选手的代码:

snail = function (array) {
var result;
while (array.length) {
result = (result ? result.concat(array.shift()) : array.shift());
for (var i = 0; i < array.length; i++) {
result.push(array[i].pop());
}
row.result = result.concat((array.pop() || []).reverse());
for (var i = array.length - 1; i >= 0; i--) {
result.push(array[i].shift());
}
}
return result;
}

代码简单,清晰明了,充分调用了数组的各种方法,pop、shift、reverse等。

还有更简单的写法,原理大同小异,可能广大网友没怎么往下看或者觉得上面选手的代码更容易读懂的原因吧,得分不高,但是代码更是简练到了极致,欣赏一下代码的艺术吧。

const snail = function (array) {
const list = [];
while (array.length) {
list.push(...array.shift(), ...array.map(row => row.pop()));
array.reverse().map(row => row.reverse());
}
return list;
}

that's all,  thanks!

最新文章

  1. float---浮动带来的影响与清除浮动带来的影响方法----在路上(20)
  2. git没有changId解决方法
  3. unity平台的预处理
  4. [原] XAF How can I change XafDisplayNameAttribute dynamically
  5. 0821找不到Command Line Utility的解决方案
  6. 去掉网址中的 html编码
  7. 026. asp.net中将图片以二进制方式保存到数据库并以HTTP流方式输出
  8. [翻译]如何用YII写出安全的WEB应用
  9. Hadoop 的常用组件一览
  10. 设计模式学习(四): 1.简单工厂 (附C#实现)
  11. HttpClient 测试web API上传文件实例
  12. 大数据入门到精通11-spark dataframe 基础操作
  13. sklearn 总结
  14. 全卷积神经网络FCN
  15. spring注入时报错::No qualifying bean of type &#39;xxx.xxMapper&#39;
  16. 如何自定义 maven中的archetype
  17. Centos 添加永久路由
  18. django模板语言转义处理
  19. cube-ui的用法
  20. 如何使用Django 启动命令行及执行脚本

热门文章

  1. Python最好IDE:Pycharm使用小技巧总结,让你写代码更为舒适
  2. 重写ThreadPoolTaskExecutor
  3. JavaFX桌面应用开发系列文章
  4. 2020-08-01:MySQL 的数据如何恢复到任意时间点?
  5. three.js 制作机房(下)
  6. C#连接Oracle数据库,通过EF自动生成与数据库表相关的实体类
  7. 个人电脑搭建ftp----------------2
  8. CODING 仪表盘功能正式推出,实现工作数据可视化!
  9. vue项目打包配置多个测试环境与生产环境,用npm命令打出不同的资源包。
  10. ASP.Net中的async+await异步编程