当前位置:网站首页>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 .
边栏推荐
- 网络安全-动态路由协议RIP
- File class (add / delete)
- PS remove watermark details
- Machine learning notes (constantly updating...)
- 小程序开发的部分功能
- [data mining] task 5: k-means/dbscan clustering: double square
- 小程序開發的部分功能
- Network security - dynamic routing protocol rip
- Groovy, "try with resources" construction alternative
- Network security - vulnerabilities and Trojans
猜你喜欢
[leetcode] 797 and 1189 (basis of graph theory)
[fluent] hero animation (hero animation use process | create hero animation core components | create source page | create destination page | page Jump)
[data mining] task 5: k-means/dbscan clustering: double square
小程序開發的部分功能
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
ByteDance data Lake integration practice based on Hudi
[data mining] task 3: decision tree classification
技术大佬准备就绪,话题C位由你决定
STM32 - Application of external interrupt induction lamp
Learn BeanShell before you dare to say you know JMeter
随机推荐
机器学习笔记(持续更新中。。。)
网络安全-钓鱼
STM32 - Application of external interrupt induction lamp
Caused by: com. fasterxml. jackson. databind. exc.MismatchedInputException: Cannot construct instance o
Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
Why can't the start method be called repeatedly? But the run method can?
Analyzing several common string library functions in C language
Comment le chef de file gère - t - il l'équipe en cas d'épidémie? Contributions communautaires
Network security - the simplest virus
Processing of tree structure data
ByteDance data Lake integration practice based on Hudi
The technology boss is ready, and the topic of position C is up to you
502 (bad gateway) causes and Solutions
String splicing function of MySQL
Return the only different value (de duplication)
网络安全-扫描
网络安全-浅谈安全威胁
Depth (penetration) selector:: v-deep/deep/ and > > >
Asian Games countdown! AI target detection helps host the Asian Games!
Technology sharing | Frida's powerful ability to realize hook functions