当前位置:网站首页>Come to sword finger offer 03. Repeated numbers in the array
Come to sword finger offer 03. Repeated numbers in the array
2022-07-27 19:40:00 【Qin Yu】
Column Preface :
| This column is mainly about algorithm training , The goal is simple . But the process needs persistence , It depends on whether we can persist to the end , Become the darker one , Find that feeling . |
The finger of the sword Offer 03. Repeated numbers in an array
Find the repeated numbers in the array .
Title Description :
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 :
Example 1:
Input :
[2, 3, 1, 0, 2, 5, 3]
Output :2 or 3
Limit :
2 <= n <= 100000
Problem solving 1:
Sort the array , Look up numbers one by one , If the same, output , The difference moves back at the same time .
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;
}
}
Time O(nlog_n)
Space O(logn)
Problem solving 2:
utilize Hashset To record the numbers in the array , If the number exists in the hash table, output , Otherwise, add the number to Hash In the table .
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;
}
}
Time complexity O(N) : Traversal array use O(N) ,HashSet Add and find elements are O(1).
Spatial complexity O(N) : HashSet Occupy O(N) The size of the extra space .

边栏推荐
- c语言:clion调试方法
- 电商商城小程序项目完整源码(微信小程序)
- Golang sets the domestic image, vscode configures the golang development environment, and vscode debugs the golang code
- c语言:14、预处理
- 开启和禁用hyper-v
- Fzu1669 right angled triangle
- 三星将推多款RISC-V架构芯片,5G毫米波射频芯片会率先采用
- MySQL learning notes (1) -- variables
- 成本高、落地难、见效慢,开源安全怎么办?
- 大佬们,ORACLE CDC,本地运行,老是遇到这个An exception occurred in
猜你喜欢
随机推荐
三星将推多款RISC-V架构芯片,5G毫米波射频芯片会率先采用
嵌入式C语言指针别名
Embedded C language pointer alias
时间复杂度和空间复杂度
Flink 算子简介
Flink简介以及运行架构
C language: 6. Simple use and precautions of pointer
Golang sets the domestic image, vscode configures the golang development environment, and vscode debugs the golang code
Kettle consolidated record data reduction
一种比读写锁更快的锁,还不赶紧认识一下
揭秘高通超声波指纹被“贴膜破解”之谜
influxDB系列(三)influxDB配置文件详解
C language: 15. Structure
kettle学习——8.2版本的资源库配置变为灰色,且没有了Connect按钮
c语言:6、指针的简单使用与注意事项
搭建阿里云+typora+Picgo图床错误分析
IIS 发生未知FastCGI错误:0x80070005
落实责任到人,广州多措并举筑牢儿童暑期“安全线”
Using vscode to build u-boot development environment
Programming jump









