Home  >  Article  >  Web Front-end  >  javascript data structure questions_javascript skills

javascript data structure questions_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:02:29953browse

There is an array [5, 3, 4, 3, 0, 3, 3], any one, find the one with the most frequency, it must meet the frequency number / array length > 0.5, if there is one, use the subscript of this number Type it out, if not return -1
var a = [5,3,4,6,3,3,3]
for(var i=0,ci,r={};ci=a[ i]; i) { if (r [ci]) {
r [ci] .idx.push (i);
r [ci] .count;
} else {
r[ci]={idx:[i],count:1}
} }
var f = -1,l=a.length;
for(var p in r) {
if(r[p].count/l>0.5)f=r[p].idx;
}
alert(f);
There is an array [5,0,-5 ,2,-4,5], play the positive and negative numbers to calculate a number, the frequency of each number
var a = [5,3,4,6,-3,3,-3]
for( var i=0,ci,r={};ci=a[i ];){
if(r[Math.abs(ci)]){
r[Math.abs(ci)] s.push(r[p]);
} }
alert(s);
There is a sequence [5,0,2,-6,4] and its subsequence can be [5,0 ],[5,0,2][2,-6],[5,0,2,-6,4] but not [5,2],[5,-6]
Find the largest sequence And, the maximum sequence of the example is [5,0,2]=7 var a = [-6,0,2,5,4],max = 0;
for(var i=0,l =a.length;i for(var j=i;j for(var tmp=0,k=i;k<=j; k ){
tmp = tmp a[k];
}
if(tmp>max) max = tmp;
}
}
alert(max);
winter Reply
First question








[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]Second question If not considered If JS is convenient for Object...


[Ctrl A select all Note:
If you need to introduce external Js, you need to refresh to execute
第三题 DP解法

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

第三个全负数的bug

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn