-
Notifications
You must be signed in to change notification settings - Fork 26
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
2015年1月30日 D6 让题目继续吧! #39
Comments
function repast (guests, seatNum) {
var a=[];
while(guests.length){a.push(guests.splice(0,seatNum));}
return a;
} |
@xinglie 好快,通过。 |
function repast(guests, seatNum){
var result = [];
var fullSeats = Math.floor(guests.length / seatNum);
var remainGuests = guests.length % seatNum;
function repastHelper(seatIndex){
if(seatIndex == fullSeats){
if(remainGuests > 0){
result.push(guests.slice(seatIndex*seatNum));
}
}
else{
result.push(guests.slice(seatIndex*seatNum, (seatIndex + 1)* seatNum));
repastHelper(seatIndex + 1);
}
}
repastHelper(0);
return result;
}
function test(){
var guests;
guests = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(repast(guests, 3));
guests = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
console.log(repast(guests, 5));
guests = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];
console.log(repast(guests, 4));
}
test(); |
如果想保留guests数组不被修改的话,可以这样
|
function repast(guests,num){
var a = [];
for(var i=0;i<Math.ceil(guests.length/num)*num;i=i+num){
a.push(guests.slice(i,i+num))
}
return a
} |
|
好久没来了,最近忙成翔。 function repast (guests, seatNum) {
return guests.reduce(function(res, cur) {
return ((!res.length || res[res.length - 1].length >= seatNum) ?
(res.push([]), res[res.length - 1].push(cur)) :
res[res.length - 1].push(cur)),
res;
}, []);
} |
function repast (guests, seatNum) {
var arr=[];
for(var i=0;i<guests.length;i+=seatNum){
arr.push(guests.slice(i,i+seatNum));
}
return arr;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
做了20年前端开发,你终于领悟赚钱不能靠打工,于是你辞职回家开了间小饭店。
你的日常是这样的,平时客人不多,就安排3个人一张桌子。
忙碌时,就安排5个人一张桌子。
作为前资深前端开发,这点问题根本难不住你:
The text was updated successfully, but these errors were encountered: