题目:

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. Linux 快捷键使用

    命令运行时使用CTRL+Z,强制当前进程转为后台,并使之停止. 1. 使进程恢复运行(后台) (1)使用命令bg Example: zuii@zuii-desktop:~/unp/tcpcliserv ...

  2. java实现在图片上编辑文本内容

    package com.yin.text; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; impor ...

  3. android webview使用心得 屏幕宽度自适应

    webview中右下角的缩放按钮能不能去掉 settings.setDisplayZoomControls(false); //隐藏webview缩放按钮 让Webview加载的页面居中显示有我知道的 ...

  4. Hadoop/Spark相关面试问题总结

    面试回来之后把其中比较重要的问题记了下来写了个总结: (答案在后面) 1.简答说一下hadoop的map-reduce编程模型 2.hadoop的TextInputFormat作用是什么,如何自定义实 ...

  5. Eclipse tomcat配置 未在Eclipse中添加.jar包出错

    JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bui ...

  6. ASP.NET Web API 框架研究 Controller创建 HttpController 类型解析 选择 创建

    上一篇介绍了HttpController的一些细节,接下来说下HttpController 类型解析.选择和创建.生产HttpController实例的生产线如下图: 一.涉及的类及源码分析 涉及的类 ...

  7. windows安装mysql 5.7

    1.下载mysql 5.7 压缩包,解压在D:\software\mysql\目录下,更名称mysql-5.7.22 ,并新建data空文件夹和my.ini文件 my.ini文件的内容 [client ...

  8. iOS笔记之UIKit_UINavigationController

    //设置导航条的样式 self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent; //默认是白色  B ...

  9. Python自动化开发 - Django【基础篇】

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为: 大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能 ...

  10. DXP快捷键记录(红色为经常用到的)

    dxp快捷键 1.设计浏览器快捷键:鼠标左击                         选择鼠标位置的文档鼠标双击                         编辑鼠标位置的文档鼠标右击   ...