当前位置:网站首页>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 .

边栏推荐
- Basic network faults and troubleshooting
- kettle8.2 安装及常见问题
- Golang sets the domestic image, vscode configures the golang development environment, and vscode debugs the golang code
- S32K系列芯片--简介
- Incluxdb series (III) detailed explanation of incluxdb configuration file
- 二叉搜索树
- SQL Server top keyword usage
- rxbinding
- C language: 5. Multidimensional array
- SQL time processing (SQL server\oracle)
猜你喜欢

Webmagic+selenium+chromedriver+jdbc垂直抓取数据。

c语言:15、结构体

golang设置国内镜像,vscode配置golang开发环境,vscode调试golang代码

Summary of APP launch in vivo application market

C language: 9. Return in main function

Using vscode to build u-boot development environment

Kettle references external scripts to complete phone number cleaning, de duplication and indentation

C language: 13. Pointer and memory

c语言:13、指针与内存

Take byte offer in four rounds and answer the interview questions
随机推荐
Technology Summit 58 Liu Yuan in the same city was invited to attend qecon 2022 global software quality & effectiveness conference
Can set be used to define local variables in OPDS SQL
英特尔发布Horse Ridge芯片:22nm工艺,能够控制多个量子位
嵌入式C语言对次数不定的循环的优化
4 轮拿下字节 Offer,面试题复盘
Optimization of embedded C language for indefinite cycles
c语言:12、gdb工具调试c程序
让你的聊天气泡丰富多彩
Debian夺回“debian.community“ 域名,喷子仍不善罢甘休
HDU1573 X问题【一元线性同余方程组】
电商商城小程序项目完整源码(微信小程序)
Basic network faults and troubleshooting
汉字查拼音微信小程序项目源码
【日常积累 - 07】cuda多版本切换
Map和Set
反超华为?爱立信已拿下超过75份5G商用合同
成本高、落地难、见效慢,开源安全怎么办?
正十七边形尺规作图可解性复数证明
应用程序池已被禁用
go-zero单体服务使用泛型简化注册Handler路由