当前位置:网站首页>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 .
边栏推荐
- Custom components, using NPM packages, global data sharing, subcontracting
- [shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
- [shutter] animation animation (animatedwidget animation use process | create animation controller | create animation | create animatedwidget animation component | animation operation)
- Redis:Redis的简单使用
- Analyzing several common string library functions in C language
- STM32 - vibration sensor control relay on
- [keil5 debugging] debug is stuck in reset_ Handler solution
- STM32 - Application of external interrupt induction lamp
- [Appendix 6 Application of reflection] Application of reflection: dynamic agent
- 网络安全-最简单的病毒
猜你喜欢
自定义组件、使用npm包、全局数据共享、分包
The technology boss is ready, and the topic of position C is up to you
可视化yolov5格式数据集(labelme json文件)
Redis: simple use of redis
MySQL学习03
STM32 - vibration sensor control relay on
Query product cases - page rendering data
Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
Virtual list
Custom components, using NPM packages, global data sharing, subcontracting
随机推荐
Some functions of applet development
PS去除水印详解
Caused by: com. fasterxml. jackson. databind. exc.MismatchedInputException: Cannot construct instance o
机器学习笔记(持续更新中。。。)
疫情當頭,作為Leader如何進行團隊的管理?| 社區征文
[error record] an error is reported in the fluent interface (no mediaquery widget ancestor found. | scaffold widgets require a mediaquery)
全链路数字化转型下,零售企业如何打开第二增长曲线
File class (add / delete)
Storage basic operation
【Camera专题】手把手撸一份驱动 到 点亮Camera
Smart management of Green Cities: Digital twin underground integrated pipe gallery platform
Why can't the start method be called repeatedly? But the run method can?
word插入公式/endnote
2022 financial product revenue ranking
[Appendix 6 Application of reflection] Application of reflection: dynamic agent
Basic operation of view
网络安全-中间人攻击
A 30-year-old software tester, who has been unemployed for 4 months, is confused and doesn't know what to do?
How do browsers render pages?
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)