[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 ...
随机推荐
- Java 注解之总结
注解是Spring和Mybatis框架所大量使用的技术,要想掌握框架相关技术,注解是必须要掌握的. 掌握注解的优势: 1.能够读懂别人写的代码,特别是框架相关的代码. 2.本来可能需要很多配置文件,需 ...
- Spring Boot使用thymeleaf模板时报异常:template might not exist or might not be accessible by any of the configured Template Resolvers
错误如下: template might not exist or might not be accessible by any of the configured Template Resolver ...
- [Python]croppic 裁剪图片的Python后台实现
import cStringIO import io import base64 from PIL import Image, ImageTk def img_crop_to_file(request ...
- hibernate使用c3p0数据源
在配置好hibernate连接数据库环境的前提下,我们进行例如以下操作就能够搭建好hibernate中使用c3p0数据源的环境了. 1). 导入 jar 包: hibernate-release-4. ...
- AppFuse 3常见问题与解决方法
非常长一段时间没做SSH项目了.近期抽出时间看了一下升级到3.x的appfuse,对新版本号使用过程中出现的一些问题进行了排查.汇总例如以下.以备后用.本文原文出处: http://blog.csdn ...
- vijos - P1279Leave-绿光(数学归纳法 + python)
P1279Leave-绿光 Accepted 标签:[显示标签] 背景 期待这一份幸运,和一份冲劲,多么奇异的际遇--. 燕姿在演唱完绿光这首歌后,出给了姿迷一个考题. 北欧有一个传说! 人一生中能看 ...
- 使用OpenCV滑动条写成的简单调色器,实时输出RGB值
好久没有写博客了,近期在看OpenCV.于是动手写了个简单的RGB调色器,在终端实时输出RGB的值.通过这个程序学习滑动条的使用.程序中主要用到cvCreateTrackbar ,其使用方法例如以下: ...
- C# 实现DataGridView分页功能
C#实现DataGridView分页功能 2010-07-17 13:45:42| 分类: C#|字号 订阅 从界面可以看到,在设计时需要一个DataGridView.BindingNavi ...
- golang import all 类似python import * 效果
import "io/ioutil" func main() { content, err = iotuil.ReadFile("somefile.txt") ...
- AMQP server localhost:5672 closed the connection. Check login credentials: Socket closed
2016-04-13 09:23:38.755 18850 INFO oslo.messaging._drivers.impl_rabbit [req-fafc8542-9403-4b5a-89d2- ...