Java实现 LeetCode 229 求众数 II(二)
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(二)的更多相关文章
- Leetcode 229.求众数II
求众数II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...
- LeetCode 229. 求众数 II(Majority Element II )
题目描述 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...
- 229. 求众数 II
Q: 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: [3 ...
- 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 ...
- Java实现 Leetcode 169 求众数
public static int majorityElement(int[] nums) { int num = nums[0], count = 1; for(int i=1;i<nums. ...
- 面试之leetcode分治-求众数,x幂等
1 leetcode50 计算 x 的 n 次幂函数. 实现 pow(x, n) ,即计算 x 的 n 次幂函数. (1)调用库函数 (2)暴力o(N) (3)分治 xxxxxx.......x ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- spark on yarn安装
网上关于spark的安装说明很多了,这里就以spark pre-build with user provided hadoop 安装包为例讲解, 下载spark pre-build with us ...
- Java并发编程实战 04死锁了怎么办?
Java并发编程文章系列 Java并发编程实战 01并发编程的Bug源头 Java并发编程实战 02Java如何解决可见性和有序性问题 Java并发编程实战 03互斥锁 解决原子性问题 前提 在第三篇 ...
- 数据库连接池Druid的介绍,配置分析对比总结
Druid的简介 Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBos ...
- Unity接入友盟分享遇到的坑
最近项目接了一下友盟分享的SDK,中间遇到了几个坑,写下几条注意事项记录一下. 接入之前需要准备友盟开发者账号,相应平台开发者账号(微信.QQ.新浪微博)等... 安卓端: 1.确保 AndroidM ...
- Spring全家桶之spring boot(四)
spring boot拦截器.过滤器.servlet和健康检查机制 spring boot拦截器 spring boot配置拦截器与原来大致相同,只是需要在拦截器的配置类上添加@Configurat ...
- 前端面试题-几个很实用的BOM属性对象方法?
什么是Bom? Bom是浏览器对象.有哪些常用的Bom属性呢? (1)location对象 location.href-- 返回或设置当前文档的URL location.search -- 返回URL ...
- class.getFields和class.getDeclareFields的区别
class.getFields的定义 返回类提供的public域包括超类的共有变量; 注: 是public,我们平时定义变量一般用的private,如果用getFields是不会获得. class.g ...
- Python 字符串内置函数(四)
# 4.类型判断# isalnum()函数检测字符串是否只由字母和数字组成.s = "this2009"; # 字符中没有空格print(s.isalnum()) # 结果:Tru ...
- java中的上下问解释以及ServletContext介绍使用
摘抄的:所谓上下文,它是用来存储系统的一些初始化信息,例如在jboss中通过配置文件指定了数据源,那么在jboss启动的时候就把这个文件的相关信息上下文中,于是在我们使用这个数据源的时候,就需要先获得 ...
- Windows10下打开MySQL服务 & 查看MySQL服务是否启动
首先 确保电脑已安装MySQL客户端 其次 以管理员方式,打开Windows PowerShell 输入: net start mysql 回车 如下图: 可以了.