当前位置:网站首页>Sword finger offer03 Repeated numbers in the array [simple]
Sword finger offer03 Repeated numbers in the array [simple]
2022-07-03 12:27:00 【Wu Liuqi】
The finger of the sword Offer 03. Repeated numbers in an array
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
Limit :
2 <= n <= 100000
JAVA Code
Common method
Two levels of traversal search for the first repeated number one by one .
class Solution {
public int findRepeatNumber(int[] nums) {
for(int i = 0;i<nums.length-1;i++){
for(int j = i+1;j<nums.length;j++){
if(nums[i]==nums[j]){
return nums[i];
}
}
}
return -1;
}
}

Map Method
Use map Storage nums data , With nums The value of map Of key, When encountering the same key Value returns the value .
class Solution {
public int findRepeatNumber(int[] nums) {
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i = 0;i<nums.length;i++){
if(map.containsKey(nums[i])){
return nums[i];
}
map.put(nums[i],i);
}
return -1;
}
}

Official methods
Use Set, Store only one value , Than Map Method is simpler to store two values
expand :Set Disordered and unrepeatable .add() Method returns false, It indicates that there are duplicate values .
class Solution {
public int findRepeatNumber(int[] nums) {
Set<Integer> set = new HashSet<Integer>();
int result = -1;
for(int num:nums){
if(!set.add(num)){
result = num;
break;
}
}
return result;
}
}

“ There are no other complicated operations about location , So just storing values is enough .”
边栏推荐
- Develop plug-ins for idea
- [download attached] password acquisition tool lazagne installation and use
- temp
- elastic_ L02_ install
- Qt+vtk+occt reading iges/step model
- Flutter Widget : KeyedSubtree
- Fluent: Engine Architecture
- Apprendre à concevoir des entités logicielles réutilisables à partir de la classe, de l'API et du cadre
- Use of atomicinteger
- 为什么我的mysql容器启动不了呢
猜你喜欢

(构造笔记)ADT与OOP

Flutter 退出登录二次确认怎么做才更优雅?

Talk about the state management mechanism in Flink framework
![[learning notes] DP status and transfer](/img/5e/59c64d2fe08b89fba2d7e1e6de2761.png)
[learning notes] DP status and transfer
![[MySQL special] read lock and write lock](/img/ac/e01c26882cc664ea2e5e731c5a8bab.png)
[MySQL special] read lock and write lock

实现验证码验证

1-2 project technology selection and structure

Itext7 uses iexternalsignature container for signature and signature verification

PHP导出word方法(一phpword)

PHP導出word方法(一mht)
随机推荐
2020-11_ Technical experience set
Adult adult adult
Apprendre à concevoir des entités logicielles réutilisables à partir de la classe, de l'API et du cadre
JVM内存模型
PHP export word method (one MHT)
(構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
Prompt unread messages and quantity before opening chat group
Laravel time zone timezone
Qt+vtk+occt reading iges/step model
Itext7 uses iexternalsignature container for signature and signature verification
[official MySQL document] deadlock
Flutter: about monitoring on flutter applications
Unicode查询的官方网站
Unicode encoding table download
ES6新特性
145. Post order traversal of binary tree
LeetCode 0556. Next bigger element III - end of step 4
Cloud Computing future - native Cloud
Dart: view the dill compiled code file
Oracle advanced (I) realize DMP by expdp impdp command