当前位置:网站首页>Sword finger offer 03 Duplicate number in array
Sword finger offer 03 Duplicate number in array
2022-04-23 17:32:00 【hequnwang10】
One 、 Title Description
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
Two 、 Problem solving
In situ hash
Put the value at the corresponding subscript , Exchange data , If there is duplication, just put back the duplicate data .
class Solution {
public int findRepeatNumber(int[] nums) {
// In situ hash
int length = nums.length;
for(int i = 0;i<length;i++){
// If there is repetition be nums[nums[i]] == nums[i]
while(nums[nums[i]] != i){
// After exchanging data , Found duplicate , Then return to
if (nums[i] == nums[nums[i]]) {
return nums[i];
}
swap(nums,nums[i],i);
}
}
return -1;
}
public void swap(int[] nums,int left,int right){
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009851.html
边栏推荐
- Ouvrir des contrats à terme, ouvrir des comptes en nuage ou faire confiance aux logiciels des sociétés à terme?
- ASP. Net core reads the configuration file in the class library project
- Use of shell cut command
- 198. 打家劫舍-动态规划
- Self use learning notes - connectingstring configuration
- 48. 旋转图像
- C dapper basically uses addition, deletion, modification and query transactions, etc
- Deep understanding of control inversion and dependency injection
- C语言函数详解
- [batch change MySQL table and corresponding codes of fields in the table]
猜你喜欢
1217_使用SCons生成目标文件
Signalr can actively send data from the server to the client
HCIP第五次实验
ClickHouse-表引擎
[ES6] promise related (event loop, macro / micro task, promise, await / await)
Bottom processing of stack memory in browser
Why do some people say SCM is simple and I have to learn it so hard?
Net standard
958. 二叉树的完全性检验
Future 用法详解
随机推荐
【WPF绑定3】 ListView基础绑定和数据模板绑定
flink 学习(十二)Allowed Lateness和 Side Output
ECMAScript history
Future 用法详解
Input file upload
El cascade and El select click elsewhere to make the drop-down box disappear
Oninput one function to control multiple oninputs (take the contents of this input box as parameters) [very practical, very practical]
01 - get to know the advantages of sketch sketch
给 el-dialog 增加拖拽功能
[WPF binding 3] listview basic binding and data template binding
Using quartz under. Net core -- preliminary understanding of [2] operations and triggers
Using quartz under. Net core -- a simple trigger of [7] operation and trigger
Baidu Map Case - Zoom component, map scale component
ASP. Net core configuration options (Part 1)
Basic case of Baidu map
[simple understanding of database]
嵌入式系统中,FLASH中的程序代码必须搬到RAM中运行吗?
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
ClickHouse-SQL 操作
Manually implement call, apply and bind functions