当前位置:网站首页>Duplicate numbers in array
Duplicate numbers in array
2022-07-25 23:24:00 【Longqi Liuhe】
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 .
Formula example 1 :
Input :
[2, 3, 1, 0, 2, 5, 3]
Output :
2 or 3
analysis :
You can use sorting , After sorting the array , Compare adjacent elements in turn , See if they are equal , Equal to return to .
You can also use hash table , Store the elements in the array into the hash table , When this element is found in the hash table when saving, it returns .
Want to find the optimal solution , Be good at using the conditions in the diagram , The title says that the array length is n And all the numbers in the array are 0-n-1 Within the scope of , You can use the subscript method , Make the element equal to its corresponding subscript by constantly exchanging elements , This makes it easy to judge repeated elements . The time complexity of the optimal solution O(n), The space complexity is O(1).
Code :
// The subscript method
public int findRepeatNumber3(int[] nums) {
for (int i = 0; i < nums.length; i++) {
// use while After the exchange , The element in this position is still not in the correct position
while (i != nums[i]){
if (nums[i] == nums[nums[i]]){
// repeat
return nums[i];
}
int k = nums[nums[i]];
nums[nums[i]] = nums[i];
nums[i] = k;
}
}
return -1;
}

边栏推荐
- Flight control implementation of four rotor aircraft "suggestions collection"
- CTS test method "suggestions collection"
- TS class
- Idea sets get and set templates to solve the naming problem of boolean type fields
- 向下扎根,向上生长,探寻华为云AI的“根”力量
- Simulate and implement common interfaces of string class
- XxE & XML external entity injection utilization and bypass
- [interface performance optimization] reasons for index failure and how to optimize SQL
- Family relationship calculator wechat applet source code
- E-commerce RPA, a magic weapon to promote easy entry
猜你喜欢

General paging function

Secure code warrior learning record (III)

wordpress去掉网站发布时间

What has Amazon cloud technology done right to become the leader of cloud AI services for three consecutive years?

@Import

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

npm+模块加载机制

电商RPA,大促轻松上阵的法宝

Week 2: convolutional neural network

Source code of YY music wechat applet imitating Netease cloud music
随机推荐
Recommended system - an embedded learning framework for numerical features in CTR prediction
js正则表达式匹配ip地址(ip地址正则表达式验证)
r语言绘图参数(R语言plot画图)
Several commonly used traversal methods
Yii2 behavior usage and its calling method
Week 2: convolutional neural network
Tencent map API request source is not authorized, this request source domain name
Rendering, filtering (filtering) and sorting of lists
2021-09-30
Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []
自定义mvc原理
Anaconda installation tutorial environment variables (how to configure environment variables)
Solve the problem phpstudy failed to import the database
[QNX Hypervisor 2.2用户手册]9.6 gdb
Network Security Learning notes-1 file upload
Wamp MySQL empty password
Which securities firm is the best and safest for beginners to open an account
学习探索-3d轮播卡片
VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.
Practical skills of easyexcel