当前位置:网站首页>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 .”
边栏推荐
- 023(【模板】最小生成树)(最小生成树)
- DEJA_VU3D - Cesium功能集 之 053-地下模式效果
- Dart: self study system
- Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
- How to deploy web pages to Alibaba cloud
- LeetCode 0556. Next bigger element III - end of step 4
- Colleagues wrote a responsibility chain model, with countless bugs
- Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
- 网上炒股开户安不安全?谁给回答一下
- laravel 时区问题timezone
猜你喜欢
Shutter widget: centerslice attribute
剑指Offer10- I. 斐波那契数列
Is BigDecimal safe to calculate the amount? Look at these five pits~~
laravel 时区问题timezone
Itext7 uses iexternalsignature container for signature and signature verification
Fluent: Engine Architecture
【附下载】密码获取工具LaZagne安装及使用
为什么我的mysql容器启动不了呢
云计算未来 — 云原生
实现验证码验证
随机推荐
Adult adult adult
Flutter 退出登录二次确认怎么做才更优雅?
[embedded] - Introduction to four memory areas
Unicode encoding table download
(construction notes) learning experience of MIT reading
pragma-pack语法与使用
Qt+vtk+occt reading iges/step model
[official MySQL document] deadlock
为什么我的mysql容器启动不了呢
Wechat applet - basic content
Display time with message interval of more than 1 minute in wechat applet discussion area
抓包整理外篇fiddler———— 会话栏与过滤器[二]
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
(construction notes) grasp learning experience
DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
Use bloc to build a page instance of shutter
Oracle advanced (I) realize DMP by expdp impdp command
QT OpenGL rotate, pan, zoom
[MySQL special] read lock and write lock
雲計算未來 — 雲原生