当前位置:网站首页>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 .”
边栏推荐
- (构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
- 剑指Offer10- I. 斐波那契数列
- Shardingsphere sub database and sub table < 3 >
- Shell: basic learning
- Dart: self study system
- PHP export word method (phpword)
- pragma-pack语法与使用
- 网上炒股开户安不安全?谁给回答一下
- Pki/ca and digital certificate
- If you can't learn, you have to learn. Jetpack compose writes an im app (II)
猜你喜欢
Kubernetes three dozen probes and probe mode
Why can't my MySQL container start
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
PHP导出word方法(一mht)
Self made pop-up input box, input text, and click to complete the event.
PHP export word method (phpword)
shardingSphere分库分表<3>
Shutter widget: centerslice attribute
Symlink(): solution to protocol error in PHP artisan storage:link on win10
Wechat applet - basic content
随机推荐
4000字超详解指针
Dart: about grpc (I)
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
Systemverilog-- OOP--对象的拷贝
Wechat applet - basic content
flinksql是可以直接客户端建表读mysql或是kafka数据,但是怎么让它自动流转计算起来呢?
Lambda表达式
adb push apk
写一个简单的nodejs脚本
Shutter widget: centerslice attribute
Talk about the state management mechanism in Flink framework
lambda与匿名内部类的区别
elastic_ L01_ summary
error: expected reference but got (raw string)
QT OpenGL texture map
If you can't learn, you have to learn. Jetpack compose writes an im app (I)
(構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
Shutter: about inheritedwidget
2.9 overview of databinding knowledge points
[MySQL special] read lock and write lock