当前位置:网站首页>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 .”
边栏推荐
- If you can't learn, you have to learn. Jetpack compose writes an im app (I)
- Pragma pack syntax and usage
- Flutter: self study system
- Dart: About zone
- DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
- 4000 word super detailed pointer
- 1-1 token
- (construction notes) grasp learning experience
- Lambda表达式
- Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
猜你喜欢
随机推荐
Flutter Widget : Flow
Basic knowledge of OpenGL (sort it out according to your own understanding)
实现验证码验证
1-1 token
Unicode查询的官方网站
Wechat applet - basic content
Why can't my MySQL container start
ES6 standard
(construction notes) ADT and OOP
手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
02_ Lock the code, and don't let the "lock" become a worry
PHP export word method (one MHT)
(数据库提权——Redis)Redis未授权访问漏洞总结
If you can't learn, you have to learn. Jetpack compose writes an im app (II)
Unicode encoding table download
Socket TCP for network communication (I)
Flutter: about monitoring on flutter applications
lambda与匿名内部类的区别
Wechat applet development - page Jump transfer parameters
【mysql专项】读锁和写锁








