当前位置:网站首页>数组中重复的数字
数组中重复的数字
2022-07-25 23:15:00 【龙崎流河】
题目:
找出数组中重复的数字,在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
式例一:
输入:
[2, 3, 1, 0, 2, 5, 3]
输出:
2 或 3
分析:
可以采用排序法,将数组排序后,依次比较相邻元素,查看是否相等,相等就返回。
也可以采用哈希表法,将数组中的元素存入哈希表中,当存入时发现哈希表有该元素则返回。
想求最优解,要擅于利用图中的条件,题目中说数组长度为n且数组中所有数字都在0-n-1范围内,可以用下标法,通过不停交换元素使得元素和它对应的下标相等,这样方便判断重复元素。最优解时间复杂度O(n),空间复杂度为O(1)。
代码:
// 下标法
public int findRepeatNumber3(int[] nums) {
for (int i = 0; i < nums.length; i++) {
//用while交换之后,该位置的元素仍然没有在正确的位置
while (i != nums[i]){
if (nums[i] == nums[nums[i]]){
//重复
return nums[i];
}
int k = nums[nums[i]];
nums[nums[i]] = nums[i];
nums[i] = k;
}
}
return -1;
}

边栏推荐
- ZCMU--5015: 完成任务
- The difference between MySQL clustered index and non clustered index
- 赋能合作伙伴,亚马逊云科技如何落地“扶上马,送一程”?
- AI chief architect 12 AICA industrial landing analysis under the industrial production process optimization scenario
- @Autowired注解 required属性
- 多模态——Deep Multi-Modal Sets
- Tencent map API request source is not authorized, this request source domain name
- @Import
- OASYS system of code audit
- Custom MVC principle
猜你喜欢

Design of Butterworth filter and drawing of amplitude frequency characteristic curve

自定义mvc原理

About using NPM command under the terminal, the installation error problem is solved (my own experience)

VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.

How to set pseudo static for WordPress fixed links
![Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []](/img/06/85a6ba450fc2637a4ac1cf6a630912.png)
Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []

MathType安装和解决不能Crtl+V的问题

Take root downward, grow upward, and explore the "root" power of Huawei cloud AI

动态内存管理

XXE&XML-外部实体注入-利用和绕过
随机推荐
Very simple vsplayaudio online music player plug-in
[interface performance optimization] reasons for index failure and how to optimize SQL
Redis expiration key deletion strategy [easy to understand]
向下扎根,向上生长,探寻华为云AI的“根”力量
Panzer_ Jack's personal blog founding day
MathType installation and solution cannot solve the problem of crtl+v
MathType安装和解决不能Crtl+V的问题
The small icon of notification setting shows a small square
Enterprise level inventory management system of code audit
Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK
Check code generation
7-1 understand everything
QT operation to solve large amount of duplicate data
Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
Analysis of the influence of ESM direction finding error on positioning error
5 ROS simulation modeling (3-rviz+gazebo+ control simulation robot)
Circle detection and line detection of PCL
CTS测试方法「建议收藏」
anaconda安装教程环境变量(如何配置环境变量)
Anaconda installation tutorial environment variables (how to configure environment variables)