当前位置:网站首页>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 .”
边栏推荐
- Fundamentals of concurrent programming (III)
- Flutter Widget : KeyedSubtree
- TOGAF认证自学宝典V2.0
- Php Export word method (One MHT)
- OpenGL index cache object EBO and lineweight mode
- 111. Minimum depth of binary tree
- 2020-09_ Shell Programming Notes
- Use bloc to build a page instance of shutter
- Talk about the state management mechanism in Flink framework
- Swagger
猜你喜欢

Unicode encoding table download

2.8 overview of ViewModel knowledge

Is BigDecimal safe to calculate the amount? Look at these five pits~~

If you can't learn, you have to learn. Jetpack compose writes an im app (II)

What is more elegant for flutter to log out and confirm again?

1-2 project technology selection and structure

Kubernetes three dozen probes and probe mode

Wechat applet - basic content

(construction notes) ADT and OOP

实现验证码验证
随机推荐
242. Effective letter heteronyms
257. All paths of binary tree
Socket TCP for network communication (I)
【mysql专项】读锁和写锁
Shell: basic learning
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
使用BLoC 构建 Flutter的页面实例
(构造笔记)GRASP学习心得
Kubectl_ Command experience set
Integer string int mutual conversion
Introduction to concurrent programming (I)
云计算未来 — 云原生
Adult adult adult
shardingSphere分库分表<3>
Qt+vtk+occt reading iges/step model
Php Export word method (One MHT)
elastic_ L01_ summary
手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
Flutter: self study system
Use of atomicinteger