【剑指 Offer】03.数组中重复的数字
题目描述
找出数组中重复的数字。
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
示例 1:
输入:
[2, 3, 1, 0, 2, 5, 3]
输出:2 或 3
限制:
2 <= n <= 100000
Java
import java.util.HashSet;
import java.util.Set;
class Solution {
public static void main(final String[] args) {
final Solution solution = new Solution();
final int[] nums = new int[] { 2, 3, 1, 0, 2, 5, 3 };
System.out.println(solution.findRepeatNumber(nums));
System.out.println(solution.findRepeatNumber2(nums));
}
/**
* 方法一:遍历数组
*
* 遍历数组中的每个元素: * 将该元素加入集合中,判断是否加入成功 * 添加失败,则找到重复数字
*
* 时间复杂度 O(n) 空间复杂度 O(n)
*
* @param nums
* @return
*/
public int findRepeatNumber(final int[] nums) {
Set<Integer> set = new HashSet<>();
int repeat = -1;
for (final int num : nums) {
if (!set.add(num)) {
repeat = num;
break;
}
}
return repeat;
}
/**
* 方法二:原地置换
* 注意所有数字都在 0~n-1 的范围内
* @param nums
* @return
*/
public int findRepeatNumber2(final int[] nums) {
int len = nums.length;
int temp = 0;
for (int i = 0; i < len; i++) {
if (nums[i] != i) {
if (nums[i] == nums[nums[i]]) {
return nums[i];
} else {
temp = nums[i];
nums[i] = nums[temp];
nums[temp] = temp;
}
}
}
return -1;
}
}
C++
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
int len = nums.size();
int temp = 0;
for (int i = 0; i < len; ++i) {
if (nums[i] != i) {
if (nums[i] == nums[nums[i]]) {
return nums[i];
} else {
temp = nums[i];
nums[i] = nums[temp];
nums[temp] = temp;
}
}
}
return -1;
}
};
int main() {
auto s = new Solution();
vector<int> v = {2, 3, 1, 0, 2, 5, 3};
cout << s->findRepeatNumber(v) << endl;
delete s;
}
Python
from __future__ import annotations
from typing import List
class Solution:
def findRepeatNumber(self, nums: List[int]) -> int:
size = len(nums)
temp = 0
for index, value in enumerate(nums):
if value != index:
if value == nums[value]:
return value
else:
temp = value
value = nums[temp]
nums[temp] = temp
return -1
if __name__ == "__main__":
s = Solution()
print(s.findRepeatNumber([2, 3, 1, 0, 2, 5, 3]))
总结
【剑指 Offer】03.数组中重复的数字的更多相关文章
- 剑指 Offer 03. 数组中重复的数字
剑指 Offer 03. 数组中重复的数字 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知 ...
- 菜鸟刷题路:剑指 Offer 03. 数组中重复的数字
剑指 Offer 03. 数组中重复的数字 哈希表/set class Solution { public int findRepeatNumber(int[] nums) { HashSet< ...
- 5.1 剑指 Offer 03. 数组中重复的数字
类型题:剑指 Offer 03. 数组中重复的数字 找出数组中重复的数字.在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了, ...
- 【剑指Offer】数组中重复的数字 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 Set 快慢指针 日期 题目地址:https://leetcod ...
- Go语言实现:【剑指offer】数组中重复的数字
该题目来源于牛客网<剑指offer>专题. 在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组 ...
- (python)剑指Offer:数组中重复的数字
问题描述 在长度为n的数组中,所有的元素都是0到n-1的范围内. 数组中的某些数字是重复的,但不知道有几个重复的数字,也不知道重复了几次,请找出任意重复的数字. 例如,输入长度为7的数组{2,3,1, ...
- 剑指Offer 50. 数组中重复的数字 (数组)
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- 剑指offer:数组中重复的数字
题目描述: 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度 ...
- [剑指Offer] 50.数组中重复的数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- 【剑指offer】数组中重复的数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
随机推荐
- 【APIO2019】桥梁(询问分块)
Description 给定一张 \(n\) 个点,\(m\) 条边的无向图,边 \(i\) 的权值为 \(d_i\).现有 \(q\) 次操作,第 \(j\) 个操作有两种模式: \(1\ b_j\ ...
- solidity 合约单元测试报错 org.fisco.bcos.web3j.protocol.exceptions.TransactionException: Transaction has failed with status: 0x16. Gas used: 1163650. (not-enough gas?)
org.fisco.bcos.web3j.protocol.exceptions.TransactionException: Transaction has failed with status: 0 ...
- http请求user_agent字段解析
浏览器的常见User Agent 各字段的解释 浏览器的User Agent字段令人迷惑,例如:某一版本的Chrome访问网络时,User Agent字段如下: Mozilla/5.0 (Window ...
- QQ电话能定位
QQ电话是网络电话,可以定位吗??? 下面介绍利用wireshark获取QQ好友IP实施精准定位 超详!! Wireshark Wireshark是一个使用WinPcap作为接口,直接与网卡进行数据报 ...
- vscode 编辑python文件
1 安装python 自动姿势 Chinese # 换成中文 path Autocomplete 路径自动补全 Vetur vue文件补全 配置文件 首选项-设置 应用程序 在 seyying.jso ...
- oracle ADG启动顺序
一.oracle ADG启动顺序 1.启动主备库监听 [oracle@dgdb1 ~]$ lsnrctl start [oracle@dgdb2 ~]$ lsnrctl start 2.启动备库 ...
- 虚拟局域网Vlan配置实战
VLAN 的基本概念 Access 类型的接口 Trunk 类型的接口 接口类型小结 以太网交换机的二层接口类型 Access口接收帧 Access口发送帧 Trunk口接收帧 Trunk口发送帧 H ...
- windows 下命令行关闭进程。
使用 进程名关闭 taskkill /im mspaint.exe /f 使用 进程id 关闭 taskkill /im 12555 /f
- java.net.BindException:Problem binding to [hostname:8088]地址已在使用
异常提示端口号被占用 查找被占用的端口 netstat -tln netstat -tln | grep 8083 netstat -tln ## 查看端口使用情况,而netstat -tln | g ...
- Exception in thread "main" org.apache.hadoop.security.AccessControlException: Permission denied: user=Mypc, access=WRITE, inode="/":fan:supergroup:drwxr-xr-x
在window上编程提示没有写Hadoop的权限 Exception in thread "main" org.apache.hadoop.security.AccessContr ...