当前位置:网站首页>【力扣】645.错误的集合
【力扣】645.错误的集合
2022-07-25 13:38:00 【aigo-2021】
集合 s 包含从 1 到 n 的整数。不幸的是,因为数据错误,导致集合里面某一个数字复制了成了集合里面的另外一个数字的值,导致集合 丢失了一个数字 并且 有一个数字重复 。
给定一个数组 nums 代表了集合 S 发生错误后的结果。
请你找出重复出现的整数,再找到丢失的整数,将它们以数组的形式返回。
示例 1:
输入:nums = [1,2,2,4]
输出:[2,3]
示例 2:
输入:nums = [1,1]
输出:[1,2]
提示:
2 <= nums.length <= 104
1 <= nums[i] <= 104
代码:
class Solution {
public int[] findErrorNums(int[] nums) {//nums:[1,2,2,4,5]
int[] arr=new int[2];//存储返回结果
int[] temp=new int[nums.length+1];//初始temp:[0,0,0,0,0,0]
for(int i=0;i<nums.length;i++){
//将nums[i]的值,作为temp的下标,放到下表对应的位置上,每放一次temp[nums[i]]的数值就加1
temp[nums[i]]++;
}//循环结束后 temp:[0,1,2,0,1,1]
//则temp数组中元素值为2对应的下标是重复数字,元素值为0对应的下标是丢失数字(第一个零除外)
for(int i=1;i<temp.length;i++){
if(temp[i]==2) arr[0]=i;
if(temp[i]==0) arr[1]=i;
}
return arr;
}
public static void main(String[] args) {
Solution s=new Solution();
System.out.println(Arrays.toString(s.findErrorNums(new int[]{1,2,2,4,5})));
}
}题目核心是利用数组的下标来表示元素,以达到不使用set集合的目的。
易错测试用例:
输入:nums=[2,2]
输出:[2,1] 而不是[2,3]
原因:未读清题目, 题中说的是 集合 s 包含从 1 到 n 的整数,也就是集合s一定是从1开始的。
边栏推荐
- Gym安装、调用以及注册
- QGIS loading online map: Gaode, Tiandi map, etc
- Canvas判断内容为空
- Online Learning and Pricing with Reusable Resources: Linear Bandits with Sub-Exponential Rewards: Li
- JS array indexof includes sort() colon sort quick sort de duplication and random sample random
- hcip第七天笔记
- Numpy简介和特点(一)
- Mu Changchun, data Research Institute of the central bank: controllable anonymity of digital RMB is an objective need to safeguard public interests and financial security
- Esp32-c3 is based on blinker lighting control 10 way switch or relay group under Arduino framework
- 并发编程之阻塞队列
猜你喜欢

arm架构移植alsa-lib和alsa-utils一路畅通

0715RHCSA

【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)

GCD details

面试官问我:Mysql的存储引擎你了解多少?
![Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component](/img/39/6f6760e80acec0b02028ea2ed1a5b1.png)
Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component

Applet sharing function

In order to improve efficiency, there are various problems when using parallelstream

二叉树基本知识

2022年下半年软考信息安全工程师如何备考?
随机推荐
Excel添加按键运行宏
The simplest solution of the whole network 1045 access denied for user [email protected] (using password:YES)
Prepare for 2022 csp-j1 2022 csp-s1 preliminaries video set
How can information security engineers prepare for the soft exam in the second half of 2022?
Congestion control of TCP
【服务器数据恢复】HP EVA服务器存储RAID信息断电丢失的数据恢复
Excel录制宏
Django 2 ----- database and admin
基于百问网IMX6ULL_PRO开发板驱动AP3216实验
0716RHCSA
G027-op-ins-rhel-04 RedHat openstack creates a customized qcow2 format image
刷题-洛谷-P1161 开灯
Int array get duplicate data
刷题-洛谷-P1150 Peter的烟
MXNet对DenseNet(稠密连接网络)的实现
【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22
Mujoco+spinningup for intensive learning training quick start
刷题-洛谷-P1047 校门外的树
Introduction and features of numpy (I)
Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets