当前位置:网站首页>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
边栏推荐
- Further study of data visualization
- In embedded system, must the program code in flash be moved to ram to run?
- Wiper component encapsulation
- 1217_使用SCons生成目标文件
- Entity Framework core captures database changes
- C# Task. Delay and thread The difference between sleep
- [related to zhengheyuan cutting tools]
- 圆环回原点问题-字节跳动高频题
- Open futures, open an account, cloud security or trust the software of futures companies?
- 1-3 components and modules
猜你喜欢
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
For the space occupation of the software, please refer to the installation directory
Deep understanding of control inversion and dependency injection
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
Halo 开源项目学习(二):实体类与数据表
快时钟同步慢时钟域下的异步控制信号slow clk to fast clk
为什么有些人说单片机简单,我学起来这么吃力?
2. Electron's HelloWorld
Using quartz under. Net core - [1] quick start
Future 用法详解
随机推荐
剑指 Offer 22. 链表中倒数第k个节点-快慢指针
Shell - introduction, variables, and basic syntax
Clickhouse SQL operation
Learning record of uni app dark horse yougou project (Part 2)
1217_使用SCons生成目标文件
Scope and scope chain in JS
239. 滑动窗口最大值(困难)-单向队列、大顶堆-字节跳动高频题
. net cross platform principle (Part I)
01-初识sketch-sketch优势
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers
198. 打家劫舍-动态规划
QT modification UI does not take effect
Indexes and views in MySQL
MySQL installation
2.Electron之HelloWorld
Open futures, open an account, cloud security or trust the software of futures companies?
440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
JVM class loading mechanism
SiteServer CMS5. 0 Usage Summary
JS, entries(), keys(), values(), some(), object Assign() traversal array usage