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. Linux内核驱动学习(一)编写最简单Linux内核模块HelloWorld

    文章目录 准备工作 什么是内核模块 编写 hello.c 模块编译 相关指令 测试结果 模块加载 模块卸载 准备工作 在进行以下操作前,首先我准备了一台电脑,并且安装了虚拟机,系统是Ubuntu16. ...

  2. [hdu4576]dp

    题意:1-n围成1圈,从1出发,第i次走a[i]步,问走m次后出现在[L,R]的概率L<=R. 思路:明显的DP,把编号变成0~n-1,令dp[i][j]表示走完i步之前停在了j上,则有dp[i ...

  3. Python 接口自动化测试

    1. 接口基础知识 1.1 接口分类 接口一般来说有两种,一种是程序内部的接口,一种是系统对外的接口. (1) webservice接口:走soap协议通过http传输,请求报文和返回报文都是xml格 ...

  4. PK,FK, UK,DF, CK

    PK 主键 constraint primary key FK 主外键关系 constraint foreign references UK 唯一约束 constraint unique key DF ...

  5. zsteg

    以前知道zsteg,但是没有去安装使用,所以就一直没有使用. 最近在找有关lsb隐写的题,发现有些大佬的wp上面写用zsteg就解决了lsb隐写的题,自己就想,难道zsteg很方便,于是就去安装zst ...

  6. java ->网络通信协议(UDP协议、TCP协议)

    网络通信协议 通过计算机网络可以使多台计算机实现连接,位于同一个网络中的计算机在进行连接和通信时需要遵守一定的规则,这就好比在道路中行驶的汽车一定要遵守交通规则一样.在计算机网络中,这些连接和通信的规 ...

  7. Vue与 Vue组件部分

    1.Vuex作用?哪种功能场景使用它? 答案:vue框架中状态管理. 场景有:单页面应用中,组件之间的状态.音乐播放. 登录状态.加入购物车 2.解释vuex最常用的两种属性 答案:分别State.G ...

  8. 容器技术之Docker基础入门

    前文我们了解了下LXC的基础用法以及图形管理工具LXC WEB Panel的简单使用,有兴趣的朋友可以参考https://www.cnblogs.com/qiuhom-1874/p/12904188. ...

  9. Pyqt5_实例1

    #coding=utf-8 ''' Created on 2018年11月2日 @author: yanerfree ''' import sys from PyQt5.QtWidgets impor ...

  10. 联通光猫管理员密码分析(HG220GS-U)

    联通光猫管理员密码分析 联通光猫型号:HG220GS-U软件版本:E00L3.03 运营商一直在做光猫防破解,对抗升级还是比较快的,所有的分析结论都和版本绑定的,因为运营商或者路由器的开发商看到后可能 ...