题目:

给定一个大小为 的数组,找出其中所有出现超过 ⌊ 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> result = new ArrayList<>();
// 黑板(用于记录数据用)
LinkedHashMap<Integer, Integer> blackboard = new LinkedHashMap<>();
// 左边记录到的位置
int leftPs = 0;
// 右边记录到的位置
int rightPs = nums.length - 1; while (leftPs <= rightPs) {
for (int i = leftPs; i <= rightPs; i++) {
if (blackboard.get(nums[i]) == null) {
blackboard.put(nums[i], 1);
if (leftPs > rightPs || 1>nums.length/3) {
result.add(nums[i]);
}
leftPs = i + 1;
break;
} else {
int count = blackboard.get(nums[i]);
count++;
if (!result.contains(nums[i]) && count>nums.length/3) {
result.add(nums[i]);
}
blackboard.put(nums[i], count);
leftPs = i + 1;
}
}
for (int i = rightPs; i >= leftPs; i--) {
if (blackboard.get(nums[i]) == null) {
blackboard.put(nums[i], 1);
if (rightPs < leftPs || 1>nums.length/3) {
result.add(nums[i]);
}
rightPs = i - 1;
break;
} else {
int count = blackboard.get(nums[i]);
count++;
if (!result.contains(nums[i]) && count>nums.length/3) {
result.add(nums[i]);
}
blackboard.put(nums[i], count);
rightPs = i - 1;
}
}
}
return result;
}
}

  

 
 
 
 
 
 
 

leetcode-229.求众数(二)的更多相关文章

  1. Java实现 LeetCode 229 求众数 II(二)

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

  2. Leetcode 229.求众数II

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

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

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

  4. 229. 求众数 II

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

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

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

  6. leetcode之求众数

    求众数 给定一个大小为 n 的数组,找到其中的众数. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 示例 2: 输入: [2,2,1,1,1,2 ...

  7. LeetCode(169. 求众数)

    问题描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  8. 【LeetCode】求众数

    给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. class Solution(object): ...

  9. 力扣(LeetCode)求众数 个人题解

    给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 ...

随机推荐

  1. jQuery+css模拟下拉框模糊搜索的实现

    html: @*输入框*@ <div> <input type="text" style="width: 85%; height: 34px;" ...

  2. powerdesigner设计的pdm模型导出清晰图片格式

    用powerdesigner设计了数据库模型,想把模型粘贴到数据库文档中,之前一直是Ctrl+A然后复制,直接粘贴过去的,这次领导说放大看不清,o(╯□╰)o 没办法,得搞个高清图复制上去啊,怎么办呢 ...

  3. SpringBoot26 RestTemplate、WebClient

    1 RestTemplate RestTemplate是在客户端访问 Restful 服务的一个核心类:RestTemplate通过提供回调方法和允许配置信息转换器来实现个性化定制RestTempla ...

  4. ./run.sh --indir examples/demo/ --outdir examples/results/ --vis

    (AlphaPose20180911) luo@luo-ThinkPad-W540:AlphaPose$ ./run.sh --indir examples/demo/ --outdir exampl ...

  5. mybatis 框架 的应用之二(批量添加、实现分页查询)

    lf-driver=com.mysql.jdbc.Driver lf-url=jdbc:mysql://localhost:3306/test lf-user=LF lf-password=LF &l ...

  6. 9-python 的ProxyHandler处理器(代理设置)

    ProxyHandler处理器(代理设置) 使用代理IP,这是爬虫/反爬虫的第二大招,通常也是最好用的. 很多网站会检测某一段时间某个IP的访问次数(通过流量统计,系统日志等),如果访问次数多的不像正 ...

  7. sql语句in超过1000时的写法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. Java List集合和Map集合的综合应用

    public static void main(String[] args) { //--------------------------------------------------------- ...

  9. Schwartz kernel theorem施瓦兹核定理

    In mathematics, the Schwartz kernel theorem is a foundational result in the theory of generalized fu ...

  10. Recurrent Neural Network(递归神经网络)

    递归神经网络(RNN),是两种人工神经网络的总称,一种是时间递归神经网络(recurrent neural network),另一种是结构递归神经网络(recursive neural network ...