当前位置:网站首页>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 .”
边栏推荐
- Introduction to concurrent programming (I)
- 写一个简单的nodejs脚本
- AOSP ~ NTP (Network Time Protocol)
- ES6新特性
- DEJA_VU3D - Cesium功能集 之 053-地下模式效果
- Integer string int mutual conversion
- Shardingsphere sub database and sub table < 3 >
- shardingSphere分库分表<3>
- Basic knowledge of OpenGL (sort it out according to your own understanding)
- 剑指Offer10- I. 斐波那契数列
猜你喜欢
使用BLoC 构建 Flutter的页面实例
【mysql专项】读锁和写锁
Basic knowledge of OpenGL (sort it out according to your own understanding)
Talk about the state management mechanism in Flink framework
【附下载】密码获取工具LaZagne安装及使用
The future of cloud computing cloud native
Shardingsphere sub database and sub table < 3 >
网络通讯之Socket-Tcp(一)
Shutter: add gradient stroke to font
ES6 standard
随机推荐
239. Sliding window maximum
Lambda表达式
PHP导出word方法(一phpword)
AOSP ~ NTP (Network Time Protocol)
Summary of development issues
在网上炒股开户可以吗?资金安全吗?
JVM memory model
Lambda expression
【附下载】密码获取工具LaZagne安装及使用
257. All paths of binary tree
repo Manifest Format
The difference between lambda and anonymous inner class
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!
flinksql是可以直接客户端建表读mysql或是kafka数据,但是怎么让它自动流转计算起来呢?
剑指Offer09. 用两个栈实现队列
Itext7 uses iexternalsignature container for signature and signature verification
MySQL time zone solution
shardingSphere分库分表<3>
Why can't my MySQL container start