当前位置:网站首页>Daily question 1: numbers that appear more than half of the time in the array (Sword finger offer39)
Daily question 1: numbers that appear more than half of the time in the array (Sword finger offer39)
2022-07-21 02:07:00 【Baking soda】
Ideas : utilize hashmap, Store the number of occurrences of each number , When the number of occurrences is greater than half the length of the array , Output the number
class Solution {
public int majorityElement(int[] nums) {
int x = (nums.length)/2;
HashMap<Integer,Integer> map = new HashMap();
for(int num : nums){
if(!map.containsKey(num)){
map.put(num,1);
}else{
map.put(num,map.get(num)+1);
}
if(map.get(num) > x){
return num;
}
}
return -1;
}
}
边栏推荐
- 【AD学习记录】覆铜
- DTOS帝拓思的3D引擎将取代游戏引擎巨兽,实现国产化替代
- 如何通过CMake为QML创建插件
- Win: use Netsh command to configure port forwarding
- [leetcode] sword finger offer 57 And are two numbers of S
- MindSpore开源周年狂欢,量子机器学习与深度分子模拟等巨量新特性来袭!
- 通过例子学C标准库<assert.h>
- 深度学习2-线性单元和梯度下降
- The common loss function in the field of hyper Division
- [leetcode] sword finger offer 58 - ii Rotate string left
猜你喜欢
DOM系列之节点操作
Grouping convolution and deep separable convolution
每日一题:数组中出现次数超过一半的数字(剑指Offer39)
波场联合储备发表公开信,强调USDD不受制于任何中心化机构
DTOS帝拓思的3D引擎将取代游戏引擎巨兽,实现国产化替代
Harbor 高可用集群设计及部署(离线安装方式、含视频)
Reinforcement Mechanism Design:With Applications to Dynamic Pricing in Sponsored Search Auction
归并排序针对性刷题
95 pages intelligent factory digitalization, intelligent planning, solutions and construction scheme 2022
PHP高级-迭代器Iterator与生成器 yield &mysql是怎么发送数据结果的
随机推荐
归并排序针对性刷题
如何通过CMake为QML创建插件
MySQL C语言连接
昇思易点通 | 经典卷积神经网络的深度学习解析
DenseNet学习笔记(核心与resnet进行对比):
【5GC】5G PDU会话以及会话类型
Laravel定时任务
【AD学习记录】覆铜
Activiti7 workflow and Alibaba components, second office OA, information management, ERP, etc
vscode setting
Understanding and applying continuous integration Tekton
To clarify the tax arrears: there is no tax arrears, and will continue to operate in compliance, rooted in China
新特性解读 | MySQL 8.0 对 GTID 的限制解除
Huawei employees revealed that this position is about to start recruiting a lot!!!
部分语音特征记录
An interesting example to illustrate the difference of emplace_back() and push_back()
Activiti7工作流和阿里巴巴组件,二开办公OA、信息管理、ERP等等
Unity shader 实现图片带圆角和边线border
Use Amazon memorydb for redis as the metadata engine of juicefs
某网站登录接口password参数还原