该算法题来自于 codewars【语言: javascript】,翻译如有误差,敬请谅解~
-
场景
- 给你一个包含一些正整数和零的数字序列(数组)。
[3,2,1,0,5,6,4,0,1,5,3,0,4,2,8,0]
它可以被分割成一些零终止的子序列,例如[3,2,1,0],[5,6,4,0] ...
- 任务
- 首先按照升序对每个子序列进行排序(不排序零,总是在结尾);
- 其次,根据它们的总和值(升序)对所有子序列进行排序。
1. 参数:sequence(数组序列)
2. 结果及注意:
1)结果为排序后的数字序列。
2)如果一些子序列具有相同的和值,则根据其原始顺序进行排序 - 例如:
sortSequence([3,2,1,0,5,6,4,0,1,5,3,0,4,2,8,0]) ,返回[1,2,3,0,1,3,5,0,2,4,8,0,4,5,6,0]
sortSequence([3,2,1,0,5,6,4,0,1,5,3,0,2,2,2,0]) ,返回[1,2,3,0,2,2,2,0,1,3,5,4,5,6,0]
sortSequence([2,2,2,0,5,6,4,0,1,5,3,0,3,2,1,0]) ,返回[2,2,2,0,1,2,3,0,1,3,5,4,5,6,0]
- 解答
- 其一
const intercept = sequence => {
let arr = [];
sequence.forEach(el=>{el == 0 ? num++ : num});
for(let i=0;i {
let arr = intercept(sequence);
let sortArr = arr.map(el => el.splice(0,el.length-1).sort((a,b)=>a-b).concat(0));
sortArr.sort((a,b) => a.reduce((r,v)=>r+v,0)-b.reduce((r,v)=>r+v,0))
return sortArr.toString().split(',').map(el=>el*1);
}
- 其二
function sortSequence(sequence){
sequence.pop();
var seqs = [[]];
for(let n of sequence) {
if(n)seqs[seqs.length-1].push(n);
else seqs.push([]);
}
seqs = seqs.map(s=>s.sort((a,b)=>a-b)).sort((a,b)=>a.reduce((s,n)=>s+n,0)-b.reduce((s,n)=>s+n,0));
var res = [];
for(let s of seqs) res = res.concat(s,0);
return res;
}
- 其三
const sortSequence = (arr) => {
const container = [];
let slice = 0;
for (let i=0; i a-b).push(sliced.shift());
container.push(sliced);
}
}
const result = container.sort((a, b) => a.reduce((acc, val) => acc + val) - b.reduce((acc, val) => acc + val)).reduce((a, b) => a.concat(b));
return result;
}
- 其四
function sortSequence(ar){
let cage = [];
const forge = ar.reduce((a,e,i) => e ? (cage.push(e),a) : (cage.sort((a,b) => a - b),cage.unshift([cage.reduce((p,v) => p + v, 0),i]),a.push(cage),cage = [],a),[]);
return forge.sort((a,b) => a[0][0] !== b[0][0] ? a[0][0] - b[0][0] : a[0][1] - b[0][1]).reduce((a,e) => (e.shift(),a.concat(e.concat(0))),[]);
}
- 其五
function sortSequence(sequence){
return (sequence+'').split(/\b0\b/)
.slice(0,-1)
.map( sub => sub.split(",").filter(x=>x).map(Number).sort((a,b)=>a-b).concat([0]) )
.reduce( (tmp,sub,i) => tmp.concat({sub:sub,sum:sum(sub),idx:i}), [])
.sort( (a,b) => a.sum-b.sum || a.idx-b.idx )
.reduce( (res,sub) => res.concat(sub.sub), [])
}
const sum = arr => arr.reduce((s,v)=>s+ +v, 0);
- 其六
function sortSequence(seq) {
var groups = seq.filter(n => n === 0).map(s => []);
var n = 0;
for (var i = 0; i < seq.length; i++) {
if (seq[i] === 0) {
groups[n].sort((a, b) => a - b);
n++;
continue;
}
groups[n].push(seq[i]);
}
groups.sort((a, b) => a.reduce((t, n) => t + n) - b.reduce((t, n) => t + n));
return groups.reduce((t, g) => {
g.push(0);
return t.concat(g);
}, []);
}
- 其七
function sortSequence(seq){
for(var sub=[],tmp=[],sum=0,i=0,j=0;ia-b),sum,j++]);tmp=[],sum=0}
else {sum+=seq[i];tmp.push(seq[i])}
}
return sub.sort((a,b)=>a[1]==b[1]?a[2]-b[2]:a[1]-b[1]).reduce((a,b)=>a.concat(b[0],0),[])
}
- 其八
function sortSequence(seq){
var arr=[],r=[], p=seq.map((a,i)=>a==0?i:null).filter(a=>a!=null); p.unshift(-1);
p.slice(1).map((a,i)=>r.push(seq.slice(p[i]+1,a).sort((a,b)=>a-b)));
r.sort((a,b)=>a.reduce((s,a)=>s+a,0)-b.reduce((s,a)=>s+a,0));
r.map(a=>arr=arr.concat(a,[0]));
return arr
}
- 其九
const plus = (v,w) => v+w ;
const bySum = (v,w) => v.reduce(plus)-w.reduce(plus) ;
const numZeroTrail = (v,w) => v && w && v-w || w-v ;
const sortSequence = a => [ ].concat( ...a.reduce( (acc,v) => ( acc.push( [ ...acc.pop(), v ] ), v || acc.push( [ ] ), acc ), [ [ ] ] ).slice(0,-1).map( v => v.sort(numZeroTrail) ).sort(bySum) ) ;