✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java
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.
给定一个数组,求其中权制最大的元素,(该元素出现超过了一半次数)。
直观想法,HashMap,很慢
- public class Solution {
- public int majorityElement(int[] nums) {
- Map map = new HashMap<Integer, Integer>();
- int len = nums.length;
- if (len == 1){
- return nums[0];
- }
- for (int num : nums){
- if (map.containsKey(num)){
- int count = (int) map.get(num);
- if ((count + 1) > len / 2){
- return num;
- } else {
- map.put(num, count + 1);
- }
- } else {
- map.put(num, 1);
- }
- }
- return -1;
- }
- }
discuss上面看到了这道题非常完善的总结:
1、排序sort
- public int majorityElement1(int[] nums) {
- Arrays.sort(nums);
- return nums[nums.length/2];
- }
2、HashMap
3、Moore’s voting algorithm(最佳算法)
很巧妙,时间O(n) ,空间O(1),就是记录当前元素以及当前元素的“胜出”数量(比其他元素多几个)。
- 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;
- }
4、位运算 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 169. Majority Element 求出现次数最多的数 --------- java的更多相关文章
- 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(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- [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 冰山查询
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java for LeetCode 169 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Java [Leetcode 169]Majority Element
题目描述: Given an array of size n, find the majority element. The majority element is the element that ...
- LeetCode 169. Majority Element (众数)
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- 清除Linux OS 缓存
1.查看内存使用情况 [root@ip---- tpch_2_17_0]# free -m total used free shared buffers cached Mem: -/+ buffers ...
- rocksDB 安装问题简单介绍
前一段时间准备测试rocksdb,按照帖子和官网的例子,在安装过程中遇到一些问题.这里给出的是在Ubuntu下安装python使用的版本. 首先,要感谢这些帖子对我的帮助: 1:http://tech ...
- Promise学习
转自:http://www.cnblogs.com/lvdabao/p/es6-promise-1.html 去年6月份, ES2015正式发布(也就是ES6,ES6是它的乳名),其中Promise被 ...
- 当在浏览器输入一个url访问后发生了什么
首先根据DNS获取该url的ip地址,ip地址的获取可能通过本地缓存,路由缓存等得到. 然后在网络层通过路由选择查找一条可达路径,最后利用tcp/ip协议来进行数据的传输. 其中在传输层将信息添加源端 ...
- TortoiseSVN文件夹及文件图标不显示解决方法
由于自己的电脑是win7(64位)的,系统安装TortoiseSVN之后,其他的功能都能正常的使用,但是就是文件夹或文件夹的左下角就是不显示图标,这个问题前一段时间就遇到了(那个时 ...
- VMware下利用ubuntu13.04建立嵌入式开发环境之三
系统环境建立完成后就要安装和配置嵌入式开始需要的工具和服务. 一般我们在交叉编译是需要的服务有:smb.tftp.telnet.nfs.ssh和x11等.下面一步步,介绍如何安装这些服务. 一.smb ...
- C++虚函数和虚函数表
前导 在上面的博文中描述了基类中存在虚函数时,基类和派生类中虚函数表的结构. 在派生类也定义了虚函数时,函数表又是怎样的结构呢? 先看下面的示例代码: #include <iostream> ...
- what we do and how we behave
It comes after a report last week revealed the "brutal" treatment of terror suspects by th ...
- 使用Excel 2007绘制甘特图
本文将教大家如何使用Excel 2007制作甘特图.Excel并未提供甘特图类型,但还是可以绘制甘特图的,方法就是通过对堆积条形图类型进行自定义,使之显示任务.任务工期和层次结构. 下面的过程可帮助创 ...
- C#利用摄像头拍照功能实现
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...