We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
题目
创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(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}).
解题思路
代码如下:
// 对等差分 function sym(args) { var arr = Array.prototype.slice.call(arguments); return arr.reduce(function(prev, curr) { prev = unique(prev); curr = unique(curr); for (var i=0; i<curr.length; i++) { if (prev.indexOf(curr[i]) === -1) { prev.push(curr[i]); } else { prev.splice(prev.indexOf(curr[i]), 1); } console.log('new: ', prev); } return prev.sort(); }); } // 数组去重 function unique(arr) { var res = []; for (var i=0; i<arr.length; i++) { if (res.indexOf(arr[i]) === -1) { res.push(arr[i]); } } return res; } // sym([1, 2, 3], [5, 2, 1, 4]); sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])
运行效果:Symmetric Difference
The text was updated successfully, but these errors were encountered:
帮大忙了,我一点也不懂“对等差分”是什么意思,从小数学很差
Sorry, something went wrong.
No branches or pull requests
题目
创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(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}).
解题思路
代码如下:
运行效果:Symmetric Difference
The text was updated successfully, but these errors were encountered: