LeetCode & Q169-Majority Element-Easy
Array Divide and Conquer Bit Manipulation
Description:
Given an array of size n, find the majority element. The majority element is the element that appears more than
⌊ n/2 ⌋times.You may assume that the array is non-empty and the majority element always exist in the array.
我的第一种解法特别蠢,想着个数超过n/2,肯定有连续的,找出连续的就行了,当然其他元素也可以连续....真的怀疑自己脑子少根筋...
好一点的解法要引入count,至于这个变量记录什么是很讲究的,为了使时间复杂度尽量降,就用一个count记录整个数组的遍历过程。count初始化为0,当当前遍历的数字与初定的major相同,count++,否则count--。major的值随count变为0后转成下一个数。其实这就是最大投票算法。
my Solution:
public class Solution {
public int majorityElement(int[] nums) {
int major = nums[0];
int count = 0;
for (int num : nums) {
if (count == 0) {
major = num;
count++;
} else if (major == num) {
count++;
} else {
count--;
}
}
return major;
}
}
在Discuss里看到有大牛一题多解了,此处膜拜一下
// Sorting
public int majorityElement1(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
// Hashtable
public int majorityElement2(int[] nums) {
Map<Integer, Integer> myMap = new HashMap<Integer, Integer>();
//Hashtable<Integer, Integer> myMap = new Hashtable<Integer, Integer>();
int ret=0;
for (int num: nums) {
if (!myMap.containsKey(num))
myMap.put(num, 1);
else
myMap.put(num, myMap.get(num)+1);
if (myMap.get(num)>nums.length/2) {
ret = num;
break;
}
}
return ret;
}
// Moore voting algorithm 就是题主的解法
public int majorityElement3(int[] nums) {
int count=0, ret = 0;
for (int num: nums) {
if (count==0)
ret = num;
if (num!=ret)
count--;
else
count++;
}
return ret;
}
// Bit manipulation
public int majorityElement(int[] nums) {
int[] bit = new int[32];
for (int num: nums)
for (int i=0; i<32; i++)
if ((num>>(31-i) & 1) == 1)
bit[i]++;
int ret=0;
for (int i=0; i<32; i++) {
bit[i]=bit[i]>nums.length/2?1:0;
ret += bit[i]*(1<<(31-i));
}
return ret;
}
LeetCode & Q169-Majority Element-Easy的更多相关文章
- 【leetcode】Majority Element (easy)(*^__^*)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode] 229. Majority Element II 多数元素 II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- LeetCode 169. Majority Element (众数)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169 Majority Element 冰山查询
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- LeetCode 169. Majority Element - majority vote algorithm (Java)
1. 题目描述Description Link: https://leetcode.com/problems/majority-element/description/ Given an array ...
- 【leetcode】Majority Element
题目概述: Given an array of size n, find the majority element. The majority element is the element that ...
- ✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- tp5.带标签的缓存 创建和清除 测试
原文:http://www.upwqy.com/details/24.html 测试设置了标签的缓存的获取方式 和清除标签缓存. 有时候我们可能会对同类型的一些数据做统一缓存.和统一清除更新处理. 那 ...
- js实现二叉树
//binary tree//add order remove findfunction tree() { var node = function(key) { this.left = null; ...
- mysql安装过程
1.到官网下载Mysql,目前最新版都是5.0以上版本,下载之后直接解压即可 2.在终端进入bin目录(如果嫌麻烦可配置环境变量,配置之后则无需进入bin目录则可敲命令),安装数据库服务:my ...
- 聊聊Linux用户态驱动设计
序言 设备驱动可以运行在内核态,也可以运行在用户态,用户态驱动的利弊网上有很多的讨论,而且有些还上升到政治性上,这里不再多做讨论.不管用户态驱动还是内核态驱动,他们都有各自的缺点.内核态驱动的问题是: ...
- NEO从入门到开窗(3) - NEO编译器
一.啰嗦两句 第一节的时候咱说了C#编译完了之后,就该NEO的编译器搞事情了.我们完全可以按这个节奏搞,手动用NEO的编译器neon编译dll文件生成指令码文件.avm.但是NEO团队给我们写智能合约 ...
- RxJS -- Subscription
Subscription是什么? 当subscribe一个observable的时候, 返回的就是一个subscription. 它是一个一次性对象(disposable), 它有一个非常重要的方法 ...
- Konckout第五个实例:各种事件绑定
点击加一: <!doctype html> <html > <head> <meta http-equiv="Content-Type" ...
- [poj2406]Power Strings_hash
Power Strings poj-2406 题目大意:询问一个字符串最多几个相同且连续的字符串构成(Eg:abababab由4个构成,abcd由1个构成). 注释:字符串长度为n,$1\le n\l ...
- hostPath Volume - 每天5分钟玩转 Docker 容器技术(148)
hostPath Volume 的作用是将 Docker Host 文件系统中已经存在的目录 mount 给 Pod 的容器.大部分应用都不会使用 hostPath Volume,因为这实际上增加了 ...
- C# MVC NPOI导出
前台: <form id="fmexp" method="post" target="_blank"> </form> ...