当前位置:网站首页>Duplicate numbers in array
Duplicate numbers in array
2022-07-27 16:25:00 【Cute rain】
Title Description : At a length of n Array of nums All the numbers in 0~n-1 Within the scope of . Some numbers in the array are repeated , But I don't know how many numbers are repeated , I don't know how many times each number has been repeated . Please find any duplicate number in the array
Method 1 description of ideas : The easiest thing to think of is the violent solution , But there is no simpler way ? We can use map, Add elements from the array to map in , When adding, first judge whether this key value is true , If it is true , Indicates that the element already exists , This element is repeated ; If it's not true , It means that it is the first time to add , Set to true .
Code implementation :
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
unordered_map<int, bool> map;
for(int num : nums) {
if(map[num]) return num;
map[num] = true;
}
return -1;
}
};
Method 2 : The time complexity and space complexity of method 1 are O(n), We can try to reduce its spatial complexity
Description of ideas : At a length of n In the array nums All the numbers in 0~n-1 Within the scope of . Description of this meaning : The index and value of array elements are one to many . therefore , Make the index of the element correspond to the value one by one . thus , You can map the corresponding value through the index , Play a role equivalent to a dictionary .
Ergodic , First encounter number xx when , Swap it to the index xx It's about ; And when you encounter numbers for the second time xx when , There must be nums[x] = xnums[x]=x , At this point, you can get a set of repeated numbers .
What we need to pay attention to here is only nums[i]==i When i Only then increases progressively , This is to ensure that the processing of some elements will not be missed until the same elements are found .
Code implementation :
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
int i = 0;
while(i < nums.size()) {
if(nums[i] == i) {
i++;
continue;
}
if(nums[nums[i]] == nums[i])
return nums[i];
swap(nums[i],nums[nums[i]]);
}
return -1;
}
};
边栏推荐
- Oracle 常用语句
- Join hands with sifive, Galanz will enter the semiconductor field! Exposure of two self-developed chips
- 爬取常见英文名
- Coding technique - Global log switch
- 插入word中的图片保持高dpi方法
- Google Chrome reversecaptcha ad blocking
- DRF learning notes (II): Data deserialization
- Introduction and use of redis
- How PHP changes a two-dimensional array into a one-dimensional array
- : 0xc0000005: an access conflict occurs when writing position 0x01458000 - to be solved
猜你喜欢

第21回---第30回

Pychart imports the existing local installation package

C channel simply implements the publishing and subscription of message queue

Excel提取重复项

Coding skills - Global exception capture & unified return body & Business exception

DRF learning notes (V): viewset

JWT简介

Mapreduce实例(一):WordCount

Excel extract duplicates

Yys mouse connector
随机推荐
Solve the problem that Flink cannot be closed normally after startup
DeFi安全之DEX与AMMs
The 4.3 billion euro cash acquisition of OSRAM failed! AMS said it would continue to acquire
DRF learning notes (II): Data deserialization
The method of inserting degree in word
重新配置cubemx后,生成的代码用IAR打开不成功
Introduction to JWT
Axure 安装图标字体元件库
Your password does not satisfy the current policy requirements (modify MySQL password policy setting simple password)
Test novice learning classic (with ideas)
第21回---第30回
企业运维安全就用行云管家堡垒机!
MapReduce instance (I): wordcount
Redis简介与使用
Have you ever used the comma operator?
Scratch crawler framework
Excel extract duplicates
Yys mouse connector
TP5 paging some small points
The first week of C language learning - the history of C language