当前位置:网站首页>《剑指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)。
边栏推荐
- R语言使用gridExtra包的grid.arrange函数将ggplot2包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
- Stm32hal library upgrades firmware based on flash analog U disk (detailed explanation)
- Gut | 香港中文大学于君组揭示吸烟改变肠道菌群并促进结直肠癌(不要吸烟)
- Understand go language context in one article
- Nestjs configuration service, configuring cookies and sessions
- Technical experts from large factories: how can engineers improve their communication skills?
- VPP three-layer network interconnection configuration
- uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。
- Hongmeng third training (project training)
- Key switch: press FN when pressing F1-F12
猜你喜欢
Use typora to draw flow chart, sequence diagram, sequence diagram, Gantt chart, etc. for detailed explanation
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
解决msvcp120d.dll和msvcr120d.dll缺失
Abandon the Internet after 00: don't want to enter a big factory after graduation, but go to the most fashionable Web3
Qt+VTK+OCCT读取IGES/STEP模型
Visual Studio 2022下载及配置OpenCV4.5.5
How to get started embedded future development direction of embedded
ftp登录时,报错“530 Login incorrect.Login failed”
PHP基础
MATLAB extrait les données numériques d'un fichier txt irrégulier (simple et pratique)
随机推荐
Using onvif protocol to operate the device
[OBS] encapsulate the basic process of OBS acquisition
phpcms 提示信息頁面跳轉showmessage
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
[OBS] configFile in ini format of OBS
The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation
PHP server interacts with redis with a large number of close_ Wait analysis
多维度监控:智能监控的数据基础
Application of high-precision indoor positioning technology in safety management of smart factory
The world's most popular font editor FontCreator tool
Mysql根据时间搜索常用方法整理
Cadence background color setting
FL Studio 20无限试用版水果编曲下载
用了这么久线程池,你真的知道如何合理配置线程数吗?
How to: configure ClickOnce trust prompt behavior
C language two-dimensional array
Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power
asyncio 警告 DeprecationWarning: There is no current event loop
Understand go language context in one article
How to become a senior digital IC Design Engineer (1-4) Verilog coding syntax: expression