当前位置:网站首页>《剑指offer 03》数组中重复的数字
《剑指offer 03》数组中重复的数字
2022-07-03 11:02:00 【爱敲代码的小邢~】
《剑指offer 03》数组中重复的数字
【leetcode链接】
【题目】
找出数组中重复的数字。
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
示例 1:
输入:
[2, 3, 1, 0, 2, 5, 3]
输出:
2 或 3
【思路】
仔细复习题目得知,nums中的所有数都在0~n-1中,所以数组中的每个数都会有一个与其值相等的下标与之对应,如下图所示:

所以我们可以通过依次匹配相应元素与下表并判断当前元素与当前元素值对应下标元素值是否相同来判断是否元素重复。
具体步骤如下:

这里要注意的是,当我们交换了元素之后,还需要检查当前元素是否与当前下标匹配,因为这步交换操作只能保证交换过去的元素与之下标对应,并不能保证交换过来的元素与这边的下标对应。而只有前面元素和下标对应了,在后面才有可能找到重复的元素。
最后,如果遍历完数组还没有找到重复的元素代表没有重复的元素。
【代码实现】
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;
}
};
【时间复杂度&空间复杂度】
对于时间复杂度,虽然代码中一个for循环套着一个while循环,但是while循环的次数是常数次,所以时间复杂度为O(N)
于时间复杂度,虽然代码中一个for循环套着一个while循环,但是while循环的次数是常数次,所以时间复杂度为O(N)
因为我们在代码中没有使用额外的空间,故空间复杂度为O(1)。
边栏推荐
- Machine learning 3.2 decision tree model learning notes (to be supplemented)
- Cadence background color setting
- ASP.NET-酒店管理系统
- Abandon the Internet after 00: don't want to enter a big factory after graduation, but go to the most fashionable Web3
- Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power
- 2022 northeast four provinces match VP record / supplementary questions
- 导师对帮助研究生顺利完成学业提出了20条劝告:第一,不要有度假休息的打算.....
- 一文搞懂Go语言Context
- Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
- After setting up ADG, instance 2 cannot start ora-29760: instance_ number parameter not specified
猜你喜欢

Leetcode 46: full arrangement

Hongmeng third training (project training)

金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~

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

MATLAB extrait les données numériques d'un fichier txt irrégulier (simple et pratique)

Mmc5603nj geomagnetic sensor (Compass example)

DS90UB949

Viewing binary bin files with notepad++ editor
![[OBS] configFile in ini format of OBS](/img/b2/0b130cee6ea884557a30e4b408f49e.png)
[OBS] configFile in ini format of OBS

Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.
随机推荐
C language AES encryption and decryption
After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power
The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.
[vtk] source code interpretation of vtkpolydatatoimagestencil
Asyncio warning deprecationwarning: there is no current event loop
uniapp实现点击加载更多
剑指offer专项32-96题做题笔记
After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good
Unity3D学习笔记5——创建子Mesh
用了这么久线程池,你真的知道如何合理配置线程数吗?
Some common terms
R语言使用aggregate函数计算dataframe数据分组聚合的均值(sum)、不设置na.rm计算的结果、如果分组中包含缺失值NA则计算结果也为NA
Solicitation for JGG special issue: spatio-temporal omics
Using onvif protocol to operate the device
CSRF
Arctangent entropy: the latest SCI paper in July 2022
DS90UB949
AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...
动态规划(区间dp)