当前位置:网站首页>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 .”
边栏推荐
- MySQL time zone solution
- Is it OK to open an account for online stock speculation? Is the fund safe?
- C language improvement article (wchar_t) character type
- Qt+vtk+occt reading iges/step model
- init. RC service failed to start
- Swagger
- Flutter Widget : Flow
- Shutter: overview of shutter architecture (excerpt)
- (构造笔记)GRASP学习心得
- ES6新特性
猜你喜欢

The future of cloud computing cloud native
![[official MySQL document] deadlock](/img/2d/04e97d696f20c2524701888ea9cd10.png)
[official MySQL document] deadlock

剑指Offer10- I. 斐波那契数列

1-2 project technology selection and structure

Basic knowledge of OpenGL (sort it out according to your own understanding)

Shutter widget: centerslice attribute

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

ES6新特性

云计算未来 — 云原生

Why can't my MySQL container start
随机推荐
Flutter Widget : Flow
Jsup crawls Baidu Encyclopedia
The difference between lambda and anonymous inner class
347. Top k high frequency elements
(構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
[learning notes] DP status and transfer
How to convert a numeric string to an integer
If you can't learn, you have to learn. Jetpack compose writes an im app (I)
TOGAF认证自学宝典V2.0
Computer version wechat applet full screen display method, mobile phone horizontal screen method.
(construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
Shell: basic learning
temp
How to deploy web pages to Alibaba cloud
DEJA_VU3D - Cesium功能集 之 054-模拟火箭发射全过程
Cloud Computing future - native Cloud
Integer string int mutual conversion
Wechat applet development - page Jump transfer parameters
01_ Using the concurrent tool class library, is thread safety safe
Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?