题目:
The set S
originally contains numbers from 1 ton
. But unfortunately, due to the data error, one of the numbers in the set got duplicated toanother number in the set, which results in repetition of one number and loss of another number.
Given an array nums
representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.
Example 1:
Input: nums = [1,2,2,4]
Output: [2,3]
Note:
1、The given array size will in the range [2,10000].
集合S最初包含从1到n的数字,但由于数据错误,集合中的一个数字被赋值到集合中的另一个数字,导致重复一个数字和另一个数字的丢失。给定一个数组nums,表示该错误之后的数据状态。先发现数字两次,然后找到丢失的数字,以数组的形式返回他们。
思路:
先发现数字两次,然后找到丢失的数字,也就是说有最终输入两个变量并以数组的形式输出,初始化两个变量为0。由于数据丢失时,一个数字被赋值到了另一个数字,判断两个数字是否相同,若相同则是发生了数据丢失错误,然后判断是否发现数字两次以及是否丢失数字。
运行结果不符,查看解决方案,与方案的思路相同,但对于书写上还是有些差异。