【剑指 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的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
随机推荐
- Android开发系列全套课程
学习地址 https://pan.baidu.com/s/12Ljy-TDL5-P0AsYdTxGw5w#list/path=%2F
- 死磕以太坊源码分析之downloader同步
死磕以太坊源码分析之downloader同步 需要配合注释代码看:https://github.com/blockchainGuide/ 这篇文章篇幅较长,能看下去的是条汉子,建议收藏 希望读者在阅读 ...
- python简单爬去前程无忧信息招聘
import sys reload(sys) sys.setdefaultencoding('utf-8') import requests import csv from BeautifulSoup ...
- JS拼接字符串太长希望换行保持html格式拼接的方法
1. 通常情况 tabPeoStr +='<tr class="tabPeo"><td>'+data[i].name+'</td><td& ...
- H3C路由器配置——静态路由
一.网络畅通条件及排错思路 1.网络畅通的条件 网络畅通的条件:数据包能去能回,也是我们排除网络故障的理论依据. 2.网络不畅通示列 ①.目标主机不可达 原因分析:可能是数据包没有到达目的地,在中途就 ...
- 4.自定义view-进度条控件
1.效果 2.实现原理 画圆,画圆弧,画文字 外部控制进度,通过invalidate()方法更新 核心代码: @Override protected void onDraw(Canvas canvas ...
- 3.自定义view-TextView变色
1.效果 2.实现原理 自定义Textview,重写onDraw方法,将画布分成两部分,用不同颜色的画笔画 核心代码: @Override protected void onDraw(Canvas c ...
- Dubbo服务调用过程源码解析④
目录 0.服务的调用 1.发送请求 2.请求编码 3.请求的解码 4.调用具体服务 5.返回调用结果 6.接收调用结果 Dubbo SPI源码解析① Dubbo服务暴露源码解析② Dubbo服务引用源 ...
- Java连接MySQL数据库——含详细步骤和代码
工具:eclipse.MySQL.MySQL连接驱动:mysql-connector-java-5.1.45.jar 首先要下载Connector/J地址:http://www.mysql.com/d ...
- 记一次jedis并发使用问题JedisException: Could not return the resource to the pool
今天线上突然发现个奇怪的问题项目第一次启动的时候redis报错JedisException: Could not return the resource to the pool 直接访问接口的时候不报 ...