645. Wrong collection
The set S
contains integers from 1 to n
. Unfortunately, due to a data error, one element in the collection was copied to the value of another element in the collection, resulting in the collection missing an integer and having a duplicate element.
Given an array nums
representing the result of the collection S
after an error has occurred. Your task is to first find the repeated integers, then find the missing integers, and return them as an array.
Example 1:
Input: nums = [1,2,2,4] Output: [2,3]
Notice:
- The length range of the given array is [2, 10000].
- The given array is unordered.
class Solution: def findErrorNums(self, nums): """ :type nums: List[int] :rtype: List[int] """ result_list = [] sum_normal_list = sum(list(range(1,len(nums) + 1))) a_set = set(nums) sum_set = sum(a_set) sum_list = sum(nums) result_list.append(sum_list - sum_set) result_list.append(sum_normal_list - sum_set) return result_list