当前位置:网站首页>来一遍《剑指Offer》03. 数组中重复的数字
来一遍《剑指Offer》03. 数组中重复的数字
2022-07-27 16:40:00 【秦 羽】
专栏前言:
| 本专栏主要是算法训练,目的很简单。但是过程需要坚持,就看能否坚持到最后,成为那个比较黑的,找到那个感觉。 |
找出数组中重复的数字。
题目描述:
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
示例:
示例 1:
输入:
[2, 3, 1, 0, 2, 5, 3]
输出:2 或 3
限制:
2 <= n <= 100000
解题1:
对数组进行排序,一前一后查找数字,如果相同则输出,不同则同时后移。
class Solution {
public int findRepeatNumber(int[] nums) {
Arrays.sort(nums);
int low = 0, fast = 1;
while(fast < nums.length){
if(nums[low] != nums[fast]){
low++;
fast++;
}else{
return nums[low];
}
}
return -1;
}
}
时间 O(nlog_n)
空间 O(logn)
解题2:
利用 Hashset 的特点来记录数组中的数字,如果哈希表中存在该数字则输出,否则将该数字加入到Hash表中。
class Solution {
public int findRepeatNumber(int[] nums) {
Set<Integer> temps = new HashSet<>();
for(int num:nums){
if(temps.contains(num)){
return num;
}
temps.add(num);
}
return -1;
}
}
时间复杂度 O(N) : 遍历数组使用 O(N) ,HashSet 添加与查找元素皆为 O(1)。
空间复杂度 O(N) : HashSet 占用 O(N) 大小的额外空间。

边栏推荐
- 2022 preparation for autumn recruitment 10W word interview sketch PDF version, with operating system and computer network interview questions
- Nacos集群部署-高可用保证
- PHP string operation
- JS common utils encapsulation
- Matrix of shell programming -- it's cute and cool
- Automatic testing of Web UI: Selenium syntax explanation is the most complete in the history
- Definition of graph traversal and depth first search and breadth first search (2)
- kettle入门级操作第一篇(读取excel、输出excel)
- webservice的疑问
- kettle学习——8.2版本的资源库配置变为灰色,且没有了Connect按钮
猜你喜欢

200行代码快速入门文档型数据库MonogoDB

200 lines of code quick start document database monogodb

kettle8.2 安装及常见问题

Express get/post/delete... Request

kettle switch / case 控件实现分类处理

Memory management A4

MySQL 02 initial experience

阿里云视频点播服务的开通和使用

IPFs obtains the public key and private key through the interface, and encrypts the storage. First bullet

Introduction to assembly language (1)
随机推荐
SQL field type conversion
Kettle consolidated record data reduction
Cyclic multi-Variate Function for Self-Supervised Image Denoising by Disentangling Noise from Image
There is a problem with the time zone when the idea connects to the database. The server returns invalid timezone is red Need to set ‘serverTimezone‘ property.
kettle JVM内存设置---效果不明显
SQL time processing (SQL server\oracle)
The understanding of string in C.
Summary of "performance test" of special test
Resource for NS2 beginner
Performance analysis of continuous time system (1) - performance index and first and second order analysis of control system
C language case: password setting and login > clear solution getchar and scanf
Questions about webservice
C language printing diamond
Technology Summit 58 Liu Yuan in the same city was invited to attend qecon 2022 global software quality & effectiveness conference
The great idea of NS2
Unity shows Kinect captured shots
Performance analysis of continuous time systems (2) - second order system performance improvement methods PID, PR
Word 2007+ tips
Nacos cluster deployment - high availability guarantee
asp. Net experience