当前位置:网站首页>Duplicate numbers in the array of sword finger offer 03
Duplicate numbers in the array of sword finger offer 03
2022-07-03 11:49:00 【Xiao Xing who likes to knock code~】
《 The finger of the sword offer 03》 Repeated numbers in an array
【leetcode link 】
【 subject 】
Find the repeated numbers in the array .
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 .
Example 1:
Input :
[2, 3, 1, 0, 2, 5, 3]
Output :
2 or 3
【 Ideas 】
Review the topic carefully and know ,nums All the numbers in the are in 0~n-1 in , So every number in the array will have a subscript equal to its value corresponding to it , As shown in the figure below :
Therefore, we can judge whether the elements are repeated by matching the corresponding elements with the following table in turn and judging whether the subscript element values corresponding to the current element and the current element values are the same .
The specific steps are as follows :
What we should pay attention to here is , When we exchange elements , You also need to check whether the current element matches the current subscript , Because this exchange operation can only ensure that the elements exchanged in the past correspond to their subscripts , There is no guarantee that the exchanged elements correspond to the subscripts here . Only the front element corresponds to the subscript , It is possible to find duplicate elements later .
Last , If the repeated elements are not found after traversing the array, it means that there are no repeated elements .
【 Code implementation 】
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
for(int i=0;i<nums.size();i++)
{
while(i!=nums[i])
{
if(nums[i]==nums[nums[i]])
return nums[i];
swap(nums[i],nums[nums[i]]);
}
}
return -1;
}
};
【 Time complexity & Spatial complexity 】
For time complexity , Although one in the code for Loop a while loop , however while The number of cycles is constant , So the time complexity is O(N)
In terms of time complexity , Although one in the code for Loop a while loop , however while The number of cycles is constant , So the time complexity is O(N)
Because we don't use extra space in the code , So the complexity of space is O(1).
边栏推荐
- .\vmware-vdiskmanager.exe -k “c:\\xxxxx.vmdk”
- 同事写了一个责任链模式,bug无数...
- previous permutation lintcode51
- Viewing binary bin files with notepad++ editor
- vulnhub之cereal
- Phpcms prompt message page Jump to showmessage
- Excel表格转到Word中,表格不超边缘纸张范围
- FL Studio 20 unlimited trial fruit arranger Download
- C language AES encryption and decryption
- 鸿蒙第四次培训
猜你喜欢
Excel表格转到Word中,表格不超边缘纸张范围
The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.
Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.
Use typora to draw flow chart, sequence diagram, sequence diagram, Gantt chart, etc. for detailed explanation
Hongmeng third training (project training)
Spl06-007 air pressure sensor (example of barometer)
Kubernetes 三打探针及探针方式
vulnhub之pyexp
ftp登录时,报错“530 Login incorrect.Login failed”
导师对帮助研究生顺利完成学业提出了20条劝告:第一,不要有度假休息的打算.....
随机推荐
previous permutation lintcode51
cgroup简介
MCDF实验1
抓包整理外篇fiddler———— 会话栏与过滤器[二]
Mmc5603nj geomagnetic sensor (Compass example)
STL教程10-容器共性和使用场景
C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
P3250 [hnoi2016] Network + [necpc2022] f.tree path tree section + segment tree maintenance heap
银泰百货点燃城市“夜经济”
Ripper of vulnhub
Machine learning 3.2 decision tree model learning notes (to be supplemented)
Programmers' entrepreneurial trap: taking private jobs
Program process management tool -go Supervisor
vulnhub之momentum
C language AES encryption and decryption
ASP.NET-酒店管理系統
Vulnhub geminiinc
在CoreOS下部署WordPress实例教程
2022 东北四省赛 VP记录/补题
. \vmware-vdiskmanager. exe -k “c:\\xxxxx.vmdk”