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]

class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> res = new ArrayList<>();
if (nums.length < 1) {
return res;
}
int num1 = nums[0];
int count1 = 0;
int num2 = nums[0];
int count2 = 0; for (int i = 0; i < nums.length; i++) {
int temp = nums[i];
if (temp == num1) {
count1++;
} else if (temp == num2) {
count2++;
} else if (count1 == 0) {
count1 = 1;
num1 = temp;
} else if (count2 == 0) {
count2 = 1;
num2 = temp;
} else {
count1--;
count2--;
}
}
count1 = 0;
count2 = 0;
int numSum = nums.length / 3;
for (int i = 0; i < nums.length; i++) {
int temp = nums[i];
if (temp == num1) {
count1++;
} else if (temp == num2) {
count2++;
}
}
if (count1 > numSum) {
res.add(num1);
}
if (num1 != num2 && count2 > numSum) {
res.add(num2);
}
return res;
}
}

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. 为什么PUSH推送要经常背锅?

    前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 自从做了推送以后,每隔一段时间就发现有各大的公司 ...

  2. 这一份MySQL书单,可以帮你搞定90%以上的面试题!

  3. 1020 Tree Traversals (25分)思路分析 + 满分代码

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  4. Java并发编程实战 05等待-通知机制和活跃性问题

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

  5. 推荐一款Python神器,5 行 Python 代码 实现一键批量扣图

    今天给大家分享一款Python装逼实用神器. 在日常生活或者工作中,经常会遇到想将某张照片中的人物抠出来,然后拼接到其他图片上去.专业点的人可以使用 PhotoShop 的"魔棒" ...

  6. sound of the genuine

    “There is something in every one of you that waits and listens for the sound of the genuine in yours ...

  7. HttpServletResponse和HttpServletRequest的简单实用

    1.HttpServletResponse web服务器接收到客户端的http请求,针对这个请求,分别创建一一个代表请求的HttpServletRequest 对象,代表响应的- -个HttpServ ...

  8. Java基础之值传递

    一.传递类型 我们从c语言开始学习程序设计语言时就知道,参数的传递类型一般有两种:值传递和引用传递.那么什么是值传递什么是引用传递呢? 值传递:指在调用方法时将实际参数的值拷贝一份传递给方法,这样方法 ...

  9. Angular核心概念之五---过滤器

    Filter:过滤器,用于在view中呈现数据时显示为另一种格式:过滤器的本质是一个函数,接收原始数据转换为新的格式进行输出: function(oldVal){ ... return newVal ...

  10. 1.2Go环境搭建之Mac

    1.下载mac版go开发工具包,源码包或是安装包都可以 //官方下载地址 https://golang.org/dl/ //下载地址在此 https://dl.google.com/go/go1.11 ...