题解 | #数组中出现次数超过一半的数字#
数字序列中某一位的数字
http://www.nowcoder.com/practice/29311ff7404d44e0b07077f4201418f5
function MoreThanHalfNum_Solution(numbers)
{
let cond = -1;
let cnt = 0;
for(let i=0; i<numbers.length;++i){
if(cnt === 0){
cond = numbers[i];
++cnt
}else{
if(cond === numbers[i]) ++cnt;
else --cnt;
}
}
cnt =0;
for(let j=0; j<numbers.length;++j){
if(cond == numbers[j]) ++cnt;
}
if(cnt > numbers.length/2){
return cond;
}
return 0
}
module.exports = {
MoreThanHalfNum_Solution : MoreThanHalfNum_Solution
};
牛客算法题 文章被收录于专栏
牛客算法题记录