题目:

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.

也就是找数组中出现次数大于一半的数字,题目保证这个数字存在。

方法一:位操作

针对数组中每一个数的每一位,计算每一位上0和1出现的次数,取出现多的作为最终数字的当前位。

代码如下:时间复杂度32n=O(n),空间复杂度O(1)

// bit操作
public int majorityElement(int[] nums) {
int temp = 0, ans = 0, count0 = 0, count1 = 0;
for (int i = 0; i < 32; i++) {
count0 = 0;
count1 = 0;
for (int j = 0; j < nums.length; j++) {
if (((nums[j] >>> i) & 1) == 1)
count1++;
else
count0++;
}
if (count1 > count0)
temp += 1;
if (i < 31)
temp >>>= 1;
}
for (int i = 0; i < 32; i++) {
ans += ((temp >> i) & 1);
if (i < 31)
ans <<= 1;
}
return temp;
}

方法二:HashMap,

空间复杂度O(n),时间复杂度O(n),相对位操作更耗时。

// 使用hashMap
public int majorityElement(int[] nums) {
int temp = 0, ans = 0, count0 = 0, count1 = 0;
Map<Integer, Integer> countMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
countMap.put(nums[i], countMap.getOrDefault(nums[i], 0) + 1);
if (countMap.get(nums[i]) > nums.length / 2)
return nums[i];
}
return 0;
}

方法三:计数法,

这个方法应该是最优解法吧。相比位操作更好。

该方法的思路是从局部思考问题,前2K个数字中,某个数无法做成majority element,但它也有可能出现很多次,但是最终在某个点上会被其他不相同的数字中和了。从后面再计数。最终找到的就是majority element。(描述的不好,直接看代码理解吧)。

假设被中和的是majority element,不用担心,因为你干掉了和你一样多的对手,在后续的子数组中,你还是大头。

假设被中和的不是,那么后续子数组中,你还是不能。

代码如下:

public int majorityElement(int[] nums) {
int count = 0;
int ans = nums[0];
for (int i : nums) {
if (count == 0)
ans = nums[i];
if (ans == nums[i])
count++;
else
count--;
}
return ans;
}

LeetCode 169. Majority Element解题方法的更多相关文章

  1. LeetCode 169 Majority Element 解题报告

    题目要求 Given an array of size n, find the majority element. The majority element is the element that a ...

  2. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  3. 23. leetcode 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  4. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  5. [LeetCode] 169. Majority Element 多数元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  6. 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...

  7. leetcode 169 Majority Element 冰山查询

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. Java for LeetCode 169 Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. Java [Leetcode 169]Majority Element

    题目描述: Given an array of size n, find the majority element. The majority element is the element that ...

随机推荐

  1. 引用数据类型(Scanner类、Random类)

    Scanner类 Scanner类是引用数据类型的一种,我们可以使用该类来完成用户键盘录入,获取到录入的数据. 引用数据类型的使用 与定义基本数据类型变量不同,引用数据类型的变量定义及赋值有一个相对固 ...

  2. POJ3255-Roadblocks(最短路)

    Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best ...

  3. web-day5

    第5章WEB05- BootStrap篇 今日任务 使用JQuery完成表单校验 使用BootStrap制作一个响应式页面 使用BootStrap制作网站首页 教学导航 教学目标 掌握什么是响应式及响 ...

  4. restfull和传统http的区别

    摘自 https://bbs.csdn.net/topics/390716580 restfull是一种风格,不是规范,也不是所谓的封装,他只是将http协议用的更彻底了,我们在普通的开发中,虽然说是 ...

  5. codeforces966 A

    这题主要就是考虑y1两侧的最近的电梯和楼梯 当时主要是考虑  如果电梯在y1和y2中间的话   那么直接做电梯就是最优解   如果在y2右边就用abs去算 然后发现其实只考虑 y1的左右两边的电梯和楼 ...

  6. C# 函数式编程及Monads.net库

    函数式编程中,一切皆为函数,这个函数一般不是类级别的,其可以保存在变量中,可以当做参数或返回值,是函数级别的抽象和重用,将函数作为可重用的基本模块,就像面向对象中一切皆为对象,把所有事物抽象为类,面向 ...

  7. 编写高质量iOS与OS X代码的52个有效方法

    第一章重点: 第一条:OC的起源 OC由smalltalk语言演化而来的语言为消息结构(messaging structure)语言,其运行时所因执行的的代码由运行环境来决定:函数调用(functio ...

  8. 从头文件中学习sfr和sbit

    1.reg52.h 头文件,它定义了单片机的一些端口物理地址. #ifndef __REG52_H__ #define __REG52_H__ /* BYTE Registers */ sfr P0 ...

  9. DOS和批处理基本命令

    http://www.cnblogs.com/leizhao/archive/2013/03/07/2949026.html 1.rem和:: rem注释命令,该命令后的内容不被执行,但能回显 ::注 ...

  10. Delphi中break,exit,abort跳出循环的比较

    http://www.delphitop.com/html/hanshu/104.html Delphi中break,exit,abort跳出循环的比较 exit: 退出函数体abort: 遇到异常, ...