当前位置:网站首页>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).
边栏推荐
- Program process management tool -go Supervisor
- 解决msvcp120d.dll和msvcr120d.dll缺失
- Xml的(DTD,xml解析,xml建模)
- FL Studio 20 unlimited trial fruit arranger Download
- VS2015的下载地址和安装教程
- Mmc5603nj geomagnetic sensor (Compass example)
- 量化计算调研
- This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
- vulnhub之raven2
- Cacti监控Redis实现过程
猜你喜欢

机器学习 3.2 决策树模型 学习笔记(待补)

The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.

Viewing binary bin files with notepad++ editor

Vulnhub narak

Redis things

2022 northeast four provinces match VP record / supplementary questions

Use typora to draw flow chart, sequence diagram, sequence diagram, Gantt chart, etc. for detailed explanation

量化计算调研

Understand go language context in one article

After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
随机推荐
uniapp实现点击加载更多
基于turtlebot3实现SLAM建图及自主导航仿真
Concurrent programming - singleton
How to get started embedded future development direction of embedded
R language uses grid of gridextra package The array function combines multiple visual images of the ggplot2 package horizontally, and the ncol parameter defines the number of columns of the combined g
The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities
Asyncio warning deprecationwarning: there is no current event loop
鸿蒙第四次培训
Numpy np.max和np.maximum实现relu函数
How PHP solves the problem of high concurrency
外插散点数据
R language uses data The table package performs data aggregation statistics, calculates window statistics, calculates the median of sliding groups, and merges the generated statistical data into the o
vulnhub之cereal
MySQL uses the method of updating linked tables with update
Machine learning 3.2 decision tree model learning notes (to be supplemented)
MCDF实验1
Viewing binary bin files with notepad++ editor
Analysis of EPS electric steering system
PHP Basics
AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...