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 ...
随机推荐
- Ubuntu 拦截并监听 power button 的关机消息
system:ubuntu 18.04 platform:rockchip 3399 board:NanoPi M4 前言 物理上的电源按键短按之后,系统直接硬关机了,导致应用程序无法保护现场,就直接 ...
- 爬虫系列 一次采集.NET WebForm网站的坎坷历程
今天接到一个活,需要统计人员的工号信息,由于种种原因不能直接连数据库 [无奈].[无奈].[无奈].采取迂回方案,写个工具自动登录网站,采集用户信息. 这也不是第一次采集ASP.NET网站,以前采集的 ...
- Android将库导入到build.gradle
如图
- redis主从复制、主从延迟知几何
本片章节主要从 redis 主从复制延迟相关知识及影响因素做简要论述. 1.配置:repl-disable-tcp-nodelay 也即是TCP 的 TCP_NODELAY 属性,决定数据的发送时机. ...
- Spring全家桶之spring boot(一)
spring boot框架抛弃了繁琐的xml配置过程,采用大量的默认配置简化我们的开发过程.使用spring boot之后就不用像以前使用ssm的时候添加那么多配置文件了,spring boot除了支 ...
- MyBatis缓存机制(一级缓存,二级缓存)
一,MyBatis一级缓存(本地缓存) My Batis 一级缓存存在于 SqlSession 的生命周期中,是SqlSession级别的缓存.在操作数据库时需要构造SqlSession对象,在对象中 ...
- HTML新特性--canvas绘图-文本
一.html5新特性--canvas绘图-文本(重点) #常用方法与属性 -ctx.strokeText(str,x,y); 绘制描边文字(空心) str:绘制文本 x,y:字符串左上角位置(以文 ...
- 一个导致JVM物理内存消耗大的Bug
概述 最近我们公司在帮一个客户查一个JVM的问题(JDK1.8.0_191-b12),发现一个系统老是被OS Kill掉,是内存泄露导致的.在查的过程中,阴差阳错地发现了JVM另外的一个Bug.这个B ...
- vscode环境配置(一)——C Program运行
ctrl + shift +p 打开应用商店 搜索 C/C++ 和 Code Runner(一键编译运行)
- vue-cli3的eslint配置问题
vue-cli3按照官网教程配置搭建后,发现每次编译,eslint都抛出错误 error: Expected indentation of 4 spaces but found 0 (indent) ...