题目:

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)

  1. // bit操作
  2. public int majorityElement(int[] nums) {
  3. int temp = 0, ans = 0, count0 = 0, count1 = 0;
  4. for (int i = 0; i < 32; i++) {
  5. count0 = 0;
  6. count1 = 0;
  7. for (int j = 0; j < nums.length; j++) {
  8. if (((nums[j] >>> i) & 1) == 1)
  9. count1++;
  10. else
  11. count0++;
  12. }
  13. if (count1 > count0)
  14. temp += 1;
  15. if (i < 31)
  16. temp >>>= 1;
  17. }
  18. for (int i = 0; i < 32; i++) {
  19. ans += ((temp >> i) & 1);
  20. if (i < 31)
  21. ans <<= 1;
  22. }
  23. return temp;
  24. }

方法二:HashMap,

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

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

方法三:计数法,

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

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

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

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

代码如下:

  1. public int majorityElement(int[] nums) {
  2. int count = 0;
  3. int ans = nums[0];
  4. for (int i : nums) {
  5. if (count == 0)
  6. ans = nums[i];
  7. if (ans == nums[i])
  8. count++;
  9. else
  10. count--;
  11. }
  12. return ans;
  13. }

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. 一天学习两个设计模式之Facade模式(外观模式,结构型模式)

    程序这东西随着时间推移,程序会越来越大,程序中的类越来越多,而且他们之间相互关联,这会导致程序结构变得越来越复杂.因此我们在使用他们时候,必须要弄清楚他们之间的关系才能使用他们. 特别是在调用大型程序 ...

  2. android 首字母迷糊查询 拼音查询 中英文混排查询

    对于这个问题,还没有动手去做,暂且查了查资料,把思路记录下来: 1. 数据库保存拼音+汉字.在插入数据库的时候将这些信息保存下来,将来可以进行首字母模糊查询,拼音查询,中英文混排查询(参考手机通讯录数 ...

  3. Hdu2841 Visible Trees 2017-06-27 22:13 24人阅读 评论(0) 收藏

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  4. poj 2192 Zipper

    题目 刚开始本来觉得可以用队列来写,但是 例如 ta te teta,ta的t先出队列那就不行了,所以还得用dp dp[i][j] 表示A前i个字符与B前j个字符是否能构成C前i+j个字符 要使 dp ...

  5. 用命令行上传本地代码到GitHub

    有两种方式上传,ssh和https,ssh老是报错=.=我用的是https 先下载git   https://git-scm.com/downloads 在代码的文件夹的同级目录中邮件打开git ba ...

  6. kafka eagel的使用

    sql语句eq: select * from "ke_test_topic" where "partition" in (0,1,2) limit 100 官网 ...

  7. day05_雷神_函数进阶

    #day05 1.迭代器 1.1可迭代对象 str,list,tuple,dict,set,range,文件句柄 等都是可迭代对象 第一种方法:在该对象中,含有__iter__方法的就是可迭代对象,遵 ...

  8. UniGUI的 TUniPageControl控件动态拖动tabsheet的实现方法

    https://blog.csdn.net/shuiying/article/details/54932518 实现可以用鼠标动态拖动tabsheet,共三个步骤: 1.在ServerModule中, ...

  9. Excel中单元格、超级链接形成超级链接单元格

    使用函数 HYPERLINK(超链接,显示文字) =HYPERLINK("http://www.cnblogs.com/Vpygamalion/","李汉超") ...

  10. 彻底卸载Oracle database 12c教程

    1.WIN+R,然后输入regedit,回车:2.在注册表中,进入目录:\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services,删除所有以oracl ...