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 ...
随机推荐
- MyBatis映射器元素
映射器是MyBatis最强大的工具,也是我们使用MyBatis时用的最多的工具,映射器中主要有增删改查四大元素,来满足不同场景的需要: 下面是主要元素的介绍: select:查询语句 ...
- Python + request + unittest实现接口测试框架
1.为什么要写代码实现接口自动化 大家知道很多接口测试工具可以实现对接口的测试,如postman.jmeter.fiddler等等,而且使用方便,那么为什么还要写代码实现接口自动化呢?工具虽然方便,但 ...
- jquery 选择器 且 或
jquery选择器具有很强大的功能,基本的使用方法随处可见,jquery还提供了更为方便的使用. 且:$("div[id^='AAA_']div[id$='_DIV']"),此选择 ...
- 11.C++-临时对象分析
首先来参考以下代码: #include <stdio.h> class Test { int mi; public: Test(int i) { mi = i; } Test() { Te ...
- java的枚举2
首先先理解一下java中枚举的本质. java的世界中一切皆是类,下面通过一个例子解释一下enum的本质: package cn.xnchall.enumeration; public class G ...
- ADS协议变量配置界面
ADS协议是倍福产品的通信协议,在双击ADS协议图标后打开如下图的配置窗口. 变量配置表中各列的含义: 报警类型: 脚本: 当设置了报类型时,在运行时如果变量值引发报警时将执行一次脚本.例如 大于报警 ...
- Python的多线程GIL浅谈
来源知乎:https://www.zhihu.com/question/23474039/answer/269526476 在介绍Python中的线程之前,先明确一个问题,Python中的多线程是假的 ...
- Python基础教程3——教你用Python做个简单的加密程序(还基础什么呀,直接来练习吧,带源码)
因为发现基础教程我之前推荐的那个网站就已经很完善了,就不重复写了,所以本汪来一起做练习吧. 一.加密原理 记得当时我学c++的时候,学到输入输出流的时候,当时王老师就教我们写了一个小的加密程序,所以这 ...
- node.js 模块化
模块是编写稍大一点点的程序 一般就会将代码模块化 在node.js中每一个文件就是一个模块,而文件路径就是模块名 怎么使用模块? 在编写某个模块是都有三个预先定义(require,exports,mo ...
- 13.HashMap TreeMap HashTable LinkedHashMap 的区别
数据库基本连接equals和hashCode详解 http://www.cnblogs.com/XMMDMW/p/6502355.html