当前位置:网站首页>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
边栏推荐
- 基于51单片机红外无线通讯仿真
- 1-5 nodejs commonjs specification
- El cascade and El select click elsewhere to make the drop-down box disappear
- Baidu Map 3D rotation and tilt angle adjustment
- 线性代数感悟之1
- Simulation of infrared wireless communication based on 51 single chip microcomputer
- Learning record of uni app dark horse yougou project (Part 2)
- 古代埃及希腊,数学用的什么进制
- MySQL installation
- Model problems of stock in and stock out and inventory system
猜你喜欢
![Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger](/img/4e/2161fc448f4af71d9b73b7de64a17f.png)
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
![Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers](/img/ec/43dddd18f0ce215f0f1a781e31f6a8.png)
Using quartz under. Net core -- job attributes and exceptions of [4] jobs and triggers

C语言函数详解

Exercise: even sum, threshold segmentation and difference (two basic questions of list object)

Detailed explanation of C webpai route

Collection of common SQL statements

Deep understanding of control inversion and dependency injection

Scope and scope chain in JS

SiteServer CMS5. 0 Usage Summary

常用SQL语句总结
随机推荐
Clickhouse table engine
2. Electron's HelloWorld
Compare the performance of query based on the number of paging data that meet the query conditions
Solution of Navicat connecting Oracle library is not loaded
958. 二叉树的完全性检验
440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
[difference between Oracle and MySQL]
Halo 开源项目学习(二):实体类与数据表
Open futures, open an account, cloud security or trust the software of futures companies?
Node template engine (EJS, art template)
Tdan over half
Perception of linear algebra 2
1-5 nodejs commonjs specification
Shell-awk命令的使用
[C#] 彻底搞明白深拷贝
Read software engineering at Google (15)
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
XTask与Kotlin Coroutine的使用对比
【WPF绑定3】 ListView基础绑定和数据模板绑定
Use of Shell sort command