[Advanced Algorithm] - Symmetric Difference
题目
创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组.
给出两个集合 (如集合 A = {1, 2, 3} 和集合 B = {2, 3, 4}), 而数学术语 "对等差分" 的集合就是指由所有只在两个集合其中之一的元素组成的集合(A △ B = C = {1, 4}). 对于传入的额外集合 (如 D = {2, 3}), 你应该安装前面原则求前两个集合的结果与新集合的对等差分集合 (C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}).
提示
Array.reduce()
Symmetric Difference
测试用例
sym([1, 2, 3], [5, 2, 1, 4])应该返回[3, 4, 5].sym([1, 2, 3], [5, 2, 1, 4])应该只包含三个元素.sym([1, 2, 5], [2, 3, 5], [3, 4, 5])应该返回[1, 4, 5]sym([1, 2, 5], [2, 3, 5], [3, 4, 5])应该只包含三个元素.sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])应该返回[1, 4, 5].sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])应该只包含三个元素.sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])应该返回[2, 3, 4, 6, 7].sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])应该只包含五个元素.sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])应该返回[1, 2, 4, 5, 6, 7, 8, 9].sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])应该只包含八个元素.
分析思路
- 由于传入参数是可变的,所以会用到
arguments,又由于arguments只有length属性,所以要把arguments转换成普通数组,代码如下:
var argArray = Array.from(arguments); - 根据题目,分析 A,B 中互相存在的元素,都需要过滤掉,用下面的代码过滤:
function diff(A, B) {
return A.filter(function(val) {
return !B.includes(val);
});
}
这个函数是去掉 A 中所有 B 拥有的元素,交换 A,B 位置,去掉B 中所有 A拥有的元素,这样既可取出题目所需:
diff(A, B).concat(diff(B, A))
3. 使用 reduce 方法实现多个数组遍历;
4. 从测试用例中发现不能出现重复的数组值返回,所以需要去重,去重函数:
function delSame(arr) {
return arr.filter(function(val, curindex) {
return curindex == arr.indexOf(val);
});
}
代码
function sym() {
// difference between set A and set B
function diff(A, B) {
return A.filter(function(val) {
return !B.includes(val);
});
}
function delSame(arr) {
return arr.filter(function(val, curindex) {
return curindex == arr.indexOf(val);
});
}
var argArray = Array.from(arguments);
return delSame(argArray.reduce(function(arr, cur) {
return diff(arr, cur).concat(diff(cur, arr)).sort();
}));
}
// test here
sym([1, 2, 3], [5, 2, 1, 4]);
[Advanced Algorithm] - Symmetric Difference的更多相关文章
- js-FCC算法-Symmetric Difference
创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组. 给出两个集合 (如集合 A = {1, 2, 3} 和集合 B = {2 ...
- FCC(ES6写法) Symmetric Difference
创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组. 给出两个集合 (如集合 A = {1, 2, 3} 和集合 B = {2 ...
- Symmetric Difference FreeCodeCamp
function sym(args) { var arr = Array.prototype.slice.call(arguments); return arr.reduce((arr1, arr2) ...
- FCC高级编程篇之Symmetric Difference
Symmetric Difference Create a function that takes two or more arrays and returns an array of the sym ...
- hackerrank---Sets - Symmetric Difference
题目链接 集合操作 附上代码: M = int(input()) m = set(map(int, raw_input().strip().split())) N = int(input()) n = ...
- Symmetric Difference
function sym(args) { //return args; var arr = []; for(var i = 0; i < arguments.length; i++){ arr. ...
- 高级算法设计讲义 Lecture Notes for Advanced Algorithm Design
(Last modification: 2012-12-17) Textbooks: (1) David Williamson, David Shmoys. The Design of Approxi ...
- [Advanced Algorithm] - Inventory Update
题目 依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新的数量. 返回当前的库存数组,且按货物 ...
- [Advanced Algorithm] - Exact Change
题目 设计一个收银程序 checkCashRegister(),其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数. ci ...
随机推荐
- HTML5本地存储——Web SQL Database与indexedDB
虽然在HTML5 WebStorage介绍了html5本地存储的Local Storage和Session Storage,这两个是以键值对存储的解决方案,存储少量数据结构很有用,但是对于大量结构化数 ...
- Python 7 列表 for 字典,嵌套
列表: 基本格式:变量名 = [元素1,元素2,元素3] 创建:A = ['访客','admin',19] 或 A = list(['armin','admin',19]), 后者更倾向于转换为 ...
- 用sqlldr导入csv文件
1.新建文件test.ctl,内容如下 load dataCHARACTERSET 'UTF16' \*指定编码格式,很重要*\ infile 'vodall.csv' append into tab ...
- EasyUI 在textbox里面输入数据敲回车后查询和普通在textbox输入数据敲回车的区别
EasyUI实现回车键触发事件 $('#id').textbox('textbox').keydown(function (e) { if (e.keyCode == 13) { alert('ent ...
- [数据结构与算法]排序算法(Python)
1.直接插入排序 给定一个数组后,从第二个元素开始,如果比第一个小,就跟他交换位置,否则不动:第三个元素如果比第二个小,把第三个跟第二个交换位置,在把第二个与第一个比较:..... def inser ...
- 【转】】}linux awk 命令详解
http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html ----------------------------------- ...
- 在win10下改变默认的输入法切换Ctrl+Space
在win10下改变默认的输入法切换Ctrl+Space 学习了:https://www.zhihu.com/question/22288432 在win10下面,有两种风格的控制面板设置: 在输入法上 ...
- LINQ体验(2)——C# 3.0新语言特性和改进(上篇)
整体来说.Visual Studio 2008和.NET 3.5是建立在.NET2.0核心的基础之上,.NET2.0核心本身将不再变化(假设不了解.NET2.0的朋友,请參看MSDN或者一些经典的书籍 ...
- MDA模型定义及扩展
Tiny框架中.对模型本向没有不论什么强制性约束,也就是说你能够把不论什么类型的对象作为模型.也不必实现不论什么接口. 因此简单的说,你定义一个类.里面有一些描写叙述业务属性或处理的内容,就能够说它是 ...
- linux各种IPC机制(进程通信)
linux各种IPC机制 (2011-07-08 16:58:35) 原文地址:linux各种IPC机制(转)作者:jianpengliu 原帖发表在IBM的developerworks网站 ...