当前位置:网站首页>leetcode961. Find the elements repeated N times in the array with length 2n
leetcode961. Find the elements repeated N times in the array with length 2n
2022-07-03 01:57:00 【2021dragon】
Give you an array of integers nums
, The array has the following properties :
nums.length == 2*n
.nums
contain n+1 A different element .nums
There happens to be an element repetition in n Time .
Find and return duplicates n The next element .
Example :
Input :nums = [1, 2, 3, 3]
Output :3
Ideas :
Finding the target element requires two iterations of the array :
- First traversal , Count the number of occurrences of each element .
- Second traversal , Return to appear n Secondary elements , That is, the target element .
The code is as follows :
class Solution {
public:
int repeatedNTimes(vector<int>& nums) {
unordered_map<int, int> countMap;
//1、 First traversal , Count the number of times
for (auto e : nums)
{
countMap[e]++;
}
//2、 Second traversal , Return to appear n Secondary elements
for (auto e : countMap)
{
if (e.second == nums.size() / 2)
return e.first;
}
return -1; // Make sure that the compilation passes ( All paths have return values )
}
};
Explain : According to the question , When executing the code, you must find an occurrence n The second element is returned , This is called execution logic , But when the code is compiled , According to the compilation logic , We must make every logic have a return value , So you need to return an arbitrary value at the end of the code , Its purpose is to make the code compile smoothly , Although we know that the code will not run here to return .
边栏推荐
- Vant implements a simple login registration module and a personal user center
- How is the mask effect achieved in the LPL ban/pick selection stage?
- Button button adaptive size of wechat applet
- Return the only different value (de duplication)
- Huakaiyun | virtual host: IP, subnet mask, gateway, default gateway
- [data mining] task 5: k-means/dbscan clustering: double square
- One of the C language practical projects is greedy snake
- 网络安全-openvas
- How to refresh the opening amount of Oracle ERP
- [camera topic] how to save OTP data in user-defined nodes
猜你喜欢
【Camera专题】OTP数据如何保存在自定义节点中
How is the mask effect achieved in the LPL ban/pick selection stage?
STM32 - GPIO input / output mode
Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
stm32F407-------IIC通讯协议
Button button adaptive size of wechat applet
【Camera专题】Camera dtsi 完全解析
One of the C language practical projects is greedy snake
[data mining] task 4:20newsgroups clustering
Smart management of Green Cities: Digital twin underground integrated pipe gallery platform
随机推荐
Ni visa fails after LabVIEW installs the third-party visa software
【Camera专题】Camera dtsi 完全解析
2022 spring "golden three silver four" job hopping prerequisites: Software Test interview questions (with answers)
Rockchip3399 start auto load driver
网络安全-密码破解
Comment le chef de file gère - t - il l'équipe en cas d'épidémie? Contributions communautaires
小程序開發的部分功能
STM32 - switch of relay control lamp
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
网络安全-ACL访问控制列表
Take you ten days to easily complete the go micro service series (II)
[data mining] task 5: k-means/dbscan clustering: double square
Performance test | script template sorting, tool sorting and result analysis
ByteDance data Lake integration practice based on Hudi
机器学习笔记(持续更新中。。。)
网络安全-木马
File class (check)
word插入公式/endnote
[keil5 debugging] debug is stuck in reset_ Handler solution
微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题