Lintcode Brush Questions Diary 645. Collection of Errors

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:

  1. The length range of the given array is [2, 10000].
  2. The given array is unordered.
Solution :
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


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325687004&siteId=291194637