Majority Element
2016-06-11 01:43
471 查看
Given an array of size n, find the majority element. The majority element is the element that appears more than
You may assume that the array is non-empty and the majority element always exist in the array.
Tags:
Array, Divide and Conquer, Bit Manipulation
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
First thought:
Loop the array and using a hash array to count for each character,
If the counter reaches to Math.floor(n/2), return the value.
However, this one will cause the Time Limited Error.
Second try:
Sort the array first,
Loop the array and use one count variable for counting nearby same character.
If the counter reaches to Math.floor(n/2), return the value.
Otherwise reset the counter and restart from a new character.
var majorityElement = function (nums) {
var len = nums.length;
var ret = nums[0];
var counter = 1, i = 0;
nums.sort();
while (i < len - 1) {
if (nums[i] === nums[i + 1]) {
counter++;
if (counter > len / 2) {
ret = nums[i];
break;
}
} else {
counter = 1;
}
i++;
}
return ret;
};
Third Try:
Sort the array, the middle one is the one we are seeking for.
Cause the majority always happen more than Math.floor(n/2) frequency.
/**
* @param {number[]} nums
* @return {number}
*/
var majorityElement = function (nums) {
nums.sort();
return nums[parseInt(nums.length / 2)];
};
⌊ n/2 ⌋times.
You may assume that the array is non-empty and the majority element always exist in the array.
Tags:
Array, Divide and Conquer, Bit Manipulation
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
First thought:
Loop the array and using a hash array to count for each character,
If the counter reaches to Math.floor(n/2), return the value.
However, this one will cause the Time Limited Error.
Second try:
Sort the array first,
Loop the array and use one count variable for counting nearby same character.
If the counter reaches to Math.floor(n/2), return the value.
Otherwise reset the counter and restart from a new character.
var majorityElement = function (nums) {
var len = nums.length;
var ret = nums[0];
var counter = 1, i = 0;
nums.sort();
while (i < len - 1) {
if (nums[i] === nums[i + 1]) {
counter++;
if (counter > len / 2) {
ret = nums[i];
break;
}
} else {
counter = 1;
}
i++;
}
return ret;
};
Third Try:
Sort the array, the middle one is the one we are seeking for.
Cause the majority always happen more than Math.floor(n/2) frequency.
/**
* @param {number[]} nums
* @return {number}
*/
var majorityElement = function (nums) {
nums.sort();
return nums[parseInt(nums.length / 2)];
};
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- 在命令行用 sort 进行排序
- javascript实现10进制转为N进制数
- 最后一次说说闭包
- Ajax
- 2019年开发人员应该学习的8个JavaScript框架
- Linux Shell - 如何使用sort与uniq命令删除重复的文本行
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因
- Canvas 在高清屏下绘制图片变模糊的解决方法
- Redux系列02:一个炒鸡简单的react+redux例子