-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
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
[js]从数组截取最大/最小的N个元素来求和 #4
Comments
######加了个需求,请不要改变原数组哈######
var values = [1, 5, 12, 4, 3];
function minimumSum(arr, num) {
//TODO:从arr里取num个最小的元素来相加,返回相加后的总和
return arr.slice(0).sort(function(a, b) {
return a > b ? 1 : a < b ? -1 : 0
}).slice(0, num).reduce(function(prev, current) {
return prev + current
}, 0)
}
function maximumSum(arr, num) {
//TODO:从arr里取num个最大的元素来相加,返回相加后的总和
return arr.slice(0).sort(function(a, b) {
return a > b ? -1 : a < b ? 1 : 0
}).slice(0, num).reduce(function(prev, current) {
return prev + current
}, 0)
} |
/* 示例:
* var values = [1, 5, 12, 4, 3]
* minimumSum(values, 2); // => 1 + 3 = 4
* maximumSum(values, 3); // => 4 + 5 + 12 = 21
*
* param1 Array , param2 Number
* return Number
*/
function minimumSum(arr, num) {
//TODO:从arr里取num个最小的元素来相加,返回相加后的总和
var _arr = arr.slice().sort(function(a,b){return a>b?1:-1});
var res = _arr.slice(0,num).reduce(function(p,c){return p+c});
console.log(res);
}
function maximumSum(arr, num) {
//TODO:从arr里取num个最大的元素来相加,返回相加后的总和
var _arr = arr.slice().sort(function(a,b){return a>b?1:-1});
var res = _arr.slice(-num,arr.length).reduce(function(p,c){return p+c});
console.log(res);
} |
加了个需求,请不要改变原数组哈 |
@bailnl 师傅, 我来了 function minimumSum(arr/*, end*/){
var argLeng = arguments.length;
var sum = 0;
if(argLeng>=1){
if(Object.prototype.toString.call(arr)==='[object Array]'){
var newArr = arr.map(Number);
var numArr = [];
for(var i=0, leng=newArr.length; i<leng; i++){
if( !isNaN(newArr[i]) ){
numArr.push(newArr[i]);
}
}
var sortedArr = numArr.sort( function(a, b){return a-b;} );
var end = arguments[1];
if(end === void 0){ end = leng; } //end缺省全部加上, 也可以考虑end缺省为0,看题目要求
else{
end = Number(end);
end = isNaN(end) ? 0 : end;
if(end<0){ //以原数组为基准, 不以sortedArr为基准
end = Math.max( Math.ceil(end)+leng, 0 );
}
else{ end = Math.min( Math.floor(end), leng ); }
}
for(var j=0; j<Math.min(end, sortedArr.length); j++){
sum = sum + sortedArr[j];
}
}
}
return sum; //参数全部缺省或Math.min(end, sortedArr.length)=0时sum为0, 也可以考虑为NaN
};
function maximumSum(arr/*, end*/){
var argLeng = arguments.length;
var sum = 0;
if(argLeng>=1){
if(Object.prototype.toString.call(arr)==='[object Array]'){
var newArr = arr.map(Number);
var numArr = [];
for(var i=0, leng=newArr.length; i<leng; i++){
if( !isNaN(newArr[i]) ){
numArr.push(newArr[i]);
}
}
var sortedArr = numArr.sort( function(a, b){return b-a;} );
var end = arguments[1];
if(end === void 0){ end = leng; } //end缺省全部加上, 也可以考虑end缺省为0, 看题目要求
else{
end = Number(end);
end = isNaN(end) ? 0 : end;
if(end<0){ //以原数组为基准, 不以sortedArr为基准
end = Math.max( Math.ceil(end)+leng, 0 );
}
else{ end = Math.min( Math.floor(end), leng ); }
}
for(var j=0; j<Math.min(end, sortedArr.length); j++){
sum = sum + sortedArr[j];
}
}
}
return sum; //参数全部缺省或Math.min(end, sortedArr.length)=0时sum为0, 也可以考虑为NaN
};
if(!Array.prototype.map){
Array.prototype.map = function(callback/*, thisCB*/){
'use strict';
if(this===null || typeof this==='undefined'){
throw new TypeError('Array.prototype.map'
+ ' is called on null or undefined');
}
if(typeof callback !== "function"){
throw new TypeError(callback + ' is not a function');
}
var len = this.length>>>0;
var res = new Array(len);
var thisCB = arguments[1];
for(var i=0; i<len; i++){
if(i in this){
res[i] = callback.call(thisCB, this[i], i, this);
}
}
return res;
}
}; |
function minimunSun(arr,num){
var newArr = [];
for(var i = 0; i< arr.length;i++){
newArr[i] = arr[i];
}
newArr.sort(function(m,n){
return m-n;
});
var result = 0;
for(var j = 0;j<num;j++){
result+=newArr[j];
}
return result;
}
function maximunSum(arr,num){
var newArr = [];
var result = 0;
for(var i = 0; i< arr.length;i++){
newArr[i] = arr[i];
}
for(var m = 0; m<newArr.length; m++){
for(var n = m+1; n < newArr.length; n++){
var p = newArr[n];
if(newArr[m]<newArr[n]){
newArr[n] = newArr[m];
newArr[m] = p;
}
}
}
for(var j = 0; j < num; j++){
result+=newArr[j];
}
return result;
} |
function sum(strategy, ary ,num) {
if(num <= 0) {
return NaN;
}
//防止死循环
if(num > ary.length) {
num = ary.length;
}
var copyOfAry = ary.slice(),
cursor = 1,
res = 0,
buffer = 0;
while(cursor <= num) {
buffer = Math[strategy].apply(this ,copyOfAry);
res += buffer;
copyOfAry.splice(copyOfAry.indexOf(buffer) ,1);
cursor++;
}
return res;
}
function maximumSum(ary ,num) {
return sum('max' ,ary ,num);
}
function minimumSum(ary ,num) {
return sum('min' ,ary ,num);
} |
大熊的版本好牛逼, fuck!! 大熊的版本: function sum(fn ,ary ,num) {
return eval(ary.slice().sort(fn).slice(0 ,num ).join('+'));
}
function minimumSum(ary ,num) {
return sum(function(a ,b) {
return a>b ? 1 : a<b ? -1 : 0;
} ,ary ,num);
}
function maximumSum(ary ,num) {
return sum(function(a, b) {
return a>b ? -1 : a<b ? 1 : 0;
} ,ary ,num);
} |
/* 示例:
* var values = [1, 5, 12, 4, 3]
* minimumSum(values, 2); // => 1 + 3 = 4
* maximumSum(values, 3); // => 4 + 5 + 12 = 21
*
* param1 Array , param2 Number
* return Number
*/
var values = [1, 5, 12, 4, 3]
minimumSum(values, 2);
maximumSum(values, 2);
function minimumSum(arr, num) {
//TODO:从arr里取num个最小的元素来相加,返回相加后的总和
var s = arr.slice(0).sort(function(a,b){
return a - b;
});
var t = s.slice(0,num).reduce(function(o, c) {
return o + c;
});
console.log('min', t);
}
function maximumSum(arr, num) {
//TODO:从arr里取num个最大的元素来相加,返回相加后的总和
var s = arr.slice(0).sort(function(a,b){
return b - a;
});
console.log(s);
var t = s.slice(0,num).reduce(function(o, c) {
return o + c;
});
console.log('max', t);
}
// |
function maximumSum(arr, num) {
if(num > arr.length) {
return arr.reduce(function(prev, cur) {
return prev + cur;
})
}
var temp = arr.slice().sort(function(a, b){
return b - a;
});
var sum = 0;
for(var i=0; i<num; i++) {
sum += temp[i];
}
return sum;
}
function minimumSum(arr, num) {
if(num > arr.length) {
return arr.reduce(function(prev, cur) {
return prev + cur;
})
}
var temp = arr.slice().sort(function(a, b){
return a - b;
});
var sum = 0;
for(var i=0; i<num; i++) {
sum += temp[i];
}
return sum;
} |
Open
昂。。我觉得直接排序完了slice一下num然后进行加和求值就好了吧 /doge (() => {
/* 示例:
* var values = [1, 5, 12, 4, 3]
* minimumSum(values, 2); // => 1 + 3 = 4
* maximumSum(values, 3); // => 4 + 5 + 12 = 21
*
* param1 Array , param2 Number
* return Number
*/
function minimumSum(arr, num) {
//TODO:从arr里取num个最小的元素来相加,返回相加后的总和
return func(arr, num, true)
}
function maximumSum(arr, num) {
//TODO:从arr里取num个最大的元素来相加,返回相加后的总和
return func(arr, num)
}
function func (arr, num, min) {
return Array.from(arr).sort(min ? (a, b) => a - b : (a, b) => b - a).slice(0, num).reduce((result, item) => result + item, 0)
}
let values = [1, 5, 12, 4, 3]
console.log(minimumSum(values, 2)) // => 1 + 3 = 4
console.log(maximumSum(values, 3)) // => 4 + 5 + 12 = 21
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
需求
minimumSum
和maximumSum
arr
参数为数组,第二个参数num
为Number类型num
来决定要从数组里取多少个“最大”或者“最小”的元素来相加minimumSum
将从数组里取最小的num个元素来相加,而maximumSum
将从数组里取最大的num个元素来相加The text was updated successfully, but these errors were encountered: