229. 求众数 II

给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。

说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1)。

示例 1:

输入: [3,2,3]

输出: [3]

示例 2:

输入: [1,1,1,3,3,2,2,2]

输出: [1,2]

  1. class Solution {
  2. public List<Integer> majorityElement(int[] nums) {
  3. List<Integer> res = new ArrayList<>();
  4. if (nums.length < 1) {
  5. return res;
  6. }
  7. int num1 = nums[0];
  8. int count1 = 0;
  9. int num2 = nums[0];
  10. int count2 = 0;
  11. for (int i = 0; i < nums.length; i++) {
  12. int temp = nums[i];
  13. if (temp == num1) {
  14. count1++;
  15. } else if (temp == num2) {
  16. count2++;
  17. } else if (count1 == 0) {
  18. count1 = 1;
  19. num1 = temp;
  20. } else if (count2 == 0) {
  21. count2 = 1;
  22. num2 = temp;
  23. } else {
  24. count1--;
  25. count2--;
  26. }
  27. }
  28. count1 = 0;
  29. count2 = 0;
  30. int numSum = nums.length / 3;
  31. for (int i = 0; i < nums.length; i++) {
  32. int temp = nums[i];
  33. if (temp == num1) {
  34. count1++;
  35. } else if (temp == num2) {
  36. count2++;
  37. }
  38. }
  39. if (count1 > numSum) {
  40. res.add(num1);
  41. }
  42. if (num1 != num2 && count2 > numSum) {
  43. res.add(num2);
  44. }
  45. return res;
  46. }
  47. }

Java实现 LeetCode 229 求众数 II(二)的更多相关文章

  1. Leetcode 229.求众数II

    求众数II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...

  2. LeetCode 229. 求众数 II(Majority Element II )

    题目描述 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...

  3. 229. 求众数 II

    Q: 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: [3 ...

  4. Java for LeetCode 229 Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  5. Java实现 Leetcode 169 求众数

    public static int majorityElement(int[] nums) { int num = nums[0], count = 1; for(int i=1;i<nums. ...

  6. 面试之leetcode分治-求众数,x幂等

    1 leetcode50 计算 x 的 n 次幂函数. 实现 pow(x, n) ,即计算 x 的 n 次幂函数. (1)调用库函数 (2)暴力o(N) (3)分治 xxxxxx.......x    ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

随机推荐

  1. spark on yarn安装

    网上关于spark的安装说明很多了,这里就以spark  pre-build with user provided hadoop 安装包为例讲解, 下载spark  pre-build with us ...

  2. Java并发编程实战 04死锁了怎么办?

    Java并发编程文章系列 Java并发编程实战 01并发编程的Bug源头 Java并发编程实战 02Java如何解决可见性和有序性问题 Java并发编程实战 03互斥锁 解决原子性问题 前提 在第三篇 ...

  3. 数据库连接池Druid的介绍,配置分析对比总结

    Druid的简介 Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBos ...

  4. Unity接入友盟分享遇到的坑

    最近项目接了一下友盟分享的SDK,中间遇到了几个坑,写下几条注意事项记录一下. 接入之前需要准备友盟开发者账号,相应平台开发者账号(微信.QQ.新浪微博)等... 安卓端: 1.确保 AndroidM ...

  5. Spring全家桶之spring boot(四)

    spring boot拦截器.过滤器.servlet和健康检查机制  spring boot拦截器 spring boot配置拦截器与原来大致相同,只是需要在拦截器的配置类上添加@Configurat ...

  6. 前端面试题-几个很实用的BOM属性对象方法?

    什么是Bom? Bom是浏览器对象.有哪些常用的Bom属性呢? (1)location对象 location.href-- 返回或设置当前文档的URL location.search -- 返回URL ...

  7. class.getFields和class.getDeclareFields的区别

    class.getFields的定义 返回类提供的public域包括超类的共有变量; 注: 是public,我们平时定义变量一般用的private,如果用getFields是不会获得. class.g ...

  8. Python 字符串内置函数(四)

    # 4.类型判断# isalnum()函数检测字符串是否只由字母和数字组成.s = "this2009"; # 字符中没有空格print(s.isalnum()) # 结果:Tru ...

  9. java中的上下问解释以及ServletContext介绍使用

    摘抄的:所谓上下文,它是用来存储系统的一些初始化信息,例如在jboss中通过配置文件指定了数据源,那么在jboss启动的时候就把这个文件的相关信息上下文中,于是在我们使用这个数据源的时候,就需要先获得 ...

  10. Windows10下打开MySQL服务 & 查看MySQL服务是否启动

    首先 确保电脑已安装MySQL客户端 其次 以管理员方式,打开Windows PowerShell 输入: net start mysql 回车 如下图: 可以了.