当前位置:网站首页>【剑指offer】---数组中的重复数字
【剑指offer】---数组中的重复数字
2022-08-10 13:36:00 【半夏而凉】
第四次打卡!!!
活动地址:CSDN21天学习挑战赛
题目描述
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组[2,3,1,0,2,5,3],那么对应的输出是2或者3。存在不合法的输入的话输出-1。
数据范围:0≤n≤10000
进阶:时间复杂度O(n) ,空间复杂度O(n)
示例
输入:[2,3,1,0,2,5,3]
返回值:2
说明:2或3都是对的
代码
方法一:两层遍历
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param numbers int整型一维数组
* @return int整型
*/
public int duplicate (int[] numbers) {
// write code here
int n=numbers.length;
if(n<2) return -1;
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(numbers[i]==numbers[j])
return numbers[i];
}
}
return -1;
}
方法二:hashmap
public int duplicate (int[] numbers) {
// write code here
int n=numbers.length;
if(n<2) return -1;
HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
for (int i=0; i<n; i++){
if(!map.containsKey(numbers[i])){
map.put(numbers[i],i);
}
else return numbers[i];
}
return -1;
}
边栏推荐
- 黑客入门,从HTB开始
- 22家!北京昌平区通报存在食品安全问题餐饮服务企业
- 1W字详解线程本地存储 ThreadLocal
- R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的gt_highlight_rows函数高亮(highlight)表格中特定的数据行、配置高亮行的特定数据列数据加粗
- 【ECCV 2022|Millions of Prizes】PSG Competition: Pursuing the "Most Comprehensive" Scene Understanding
- 作业
- BEVDet4D: Exploit Temporal Cues in Multi-camera 3D Object Detection 论文笔记
- Existing in the rain of PFAS chemical poses a threat to the safety of drinking water
- 代码随想录笔记_动态规划_70爬楼梯
- 接口自动化测试基础篇
猜你喜欢

Send a post request at the front desk can't get the data

Error: Rule can only have one resource source (provided resource and test + include + exclude)

Short read or OOM loading DB. Unrecoverable error, aborting now

友邦人寿可观测体系设计与落地

Redis上云迁移实践

记录几道整型提升的题目

交换机的基础知识

2022-08-09: What does the following go code output?A: No, it will panic; B: Yes, it can run correctly; C: Not sure, see the voting result.package main import (“fmt“ “syn

shell:正则表达式及三剑客grep命令
![ArcMAP has a problem of -15 and cannot be accessed [Provide your license server administrator with the following information:Err-15]](/img/da/b49d7ba845c351cefc4efc174de995.png)
ArcMAP has a problem of -15 and cannot be accessed [Provide your license server administrator with the following information:Err-15]
随机推荐
2022年五大云虚拟化趋势
学习日记8
tampercfg内核模块导致机器频繁crash
Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer 论文笔记
简单的写一个防抖跟节流
Wirshark common operations and tcp three-way handshake process example analysis
【MinIO】工具类使用
Network Saboteur
vivado闪退或者message无显示
一汽奥迪:持续34年聚焦品质与体验 立足市场需求推进产品迭代
leetcode 739. Daily Temperatures 每日温度(中等)
Circle 创始人回应美财政部禁止 Tornado :隐私与安全之间关系紧张
Redis 定长队列的探索和实践
leetcode 739. Daily Temperatures 每日温度(中等)
这一次,话筒给你:向自由软件之父斯托曼 提问啦!
Short read or OOM loading DB. Unrecoverable error, aborting now
How to describe multiple paragraphs with different font settings in Open Office XML format
[219] The training course notes of the go engineer with more than 3,000 MOOCs 02 Programming ideas in the go language
C# WPF image is displayed without problems, but the solution does not display the image at runtime
友邦人寿可观测体系设计与落地