Codewars-Javascript训练手册:数组(上)
2016-04-20 13:28
441 查看
Directions Reduction
题目描述(Description):
write a function dirReduc which will take an array of strings and returns an array of strings with the needless directions removed (W<->E or S<->N side by side).In [“NORTH”, “EAST”, “WEST”, “SOUTH”, “WEST”, “WEST”], “NORTH” and “SOUTH” are not directly opposite but they become directly opposite after the reduction of “EAST” and “WEST” so the whole path is reducible to [“WEST”, “WEST”].
只有相邻以及抵消后相邻的的两个相反方向可以抵消
题目注解(Note):
All paths can’t be made simpler. The path [“NORTH”, “WEST”, “SOUTH”, “EAST”] is not reducible. “NORTH” and “WEST”, “WEST” and “SOUTH”, “SOUTH” and “EAST” are not directly opposite of each other and can’t become such. Hence the result path is itself : [“NORTH”, “WEST”, “SOUTH”, “EAST”].Solution:
某论坛大神的解决方案
function dirReduc(directions) { //判断是否为数组,不为空 if (!directions || !directions instanceof Array) return directions; var relations = {'WEST': 'EAST', 'EAST': 'WEST', 'SOUTH': 'NORTH', 'NORTH': 'SOUTH'}; for (var i = 0, length = directions.length, expect; i < length; i++) { if (directions[i] === expect) { directions.splice(i-1, 2); i -= 2; length -= 2; } //得到本次循环数组值的相反方向值 expect = relations[directions[i]]; } return directions;
直达地址
Codewars排名最高的答案:
function dirReduc(plan) { var opposite = { 'NORTH': 'SOUTH', 'EAST': 'WEST', 'SOUTH': 'NORTH', 'WEST': 'EAST'}; return plan.reduce(function(dirs, dir){ if (dirs[dirs.length - 1] === opposite[dir]) dirs.pop(); else dirs.push(dir); return dirs; }, []); }
知识点:
splice()方法:向/从数组中添加/删除项目,然后返回被删除的项目(新的数组)。
注释:该方法会改变原始数组。
语法
arrayObject.splice(index,howmany,item1,…..,itemX)
splice() 方法与 slice() 方法的作用是不同的,splice() 方法会直接对数组进行修改。参考链接
reduce (Array)(JavaScript)方法:对数组中的所有元素调用指定的回调函数。该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供。
语法
array1.reduce(callbackfn[, initialValue])
返回值为通过最后一次调用回调函数获得的累积结果。参考链接
Tribonacci Sequence(类斐波那契数列)
题目描述
create a fibonacci function that given a signature array/list, returns the first n elements - signature included of the so seeded sequence.Signature will always contain 3 numbers; n will always be a non-negative number; if n==0, then return an empty array and be ready for anything else which is not clearly specified ;)
数组Signature 总包含3个数值,n为非负整数,当n=0的时候,返回一个空数组。
Soluation:
function tribonacci(signature,n){ //判断n<=3的情况,即使数组signature有3个数值,但n为0时也只能返回空数组,用arr.slice(); if(n<=3){ return signature.slice(0,n); }else{ //累加前三项值,并将其push到数组 for(var i=3;i<n;i++){ signature.push(signature[i-3]+signature[i-2]+signature[i-1]); } return signature; } }
Codewars 得票最高的答案:
function tribonacci(signature,n){ for (var i = 0; i < n-3; i++) { // iterate n times signature.push(signature[i] + signature[i+1] + signature[i+2]); // add last 3 array items and push to trib } //优化算法,去掉了if语句。 return signature.slice(0, n); //return trib - length of n }
这就是与高手之间的差距啊。
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- 最后一次说说闭包
- Ajax
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因
- Canvas 在高清屏下绘制图片变模糊的解决方法
- Redux系列02:一个炒鸡简单的react+redux例子
- JavaScript 各种遍历方式详解
- call/apply/bind 的理解与实例分享