【Leetcode】Set Mismatch

The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another 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.

1 第一种方法,用m = collection.Counter[nums]的对nums计数,再遍历nums中的数,找出m[num]==2的num即是第一个数

2 得出原始list的sum,再得出error list的sum,通过和first数结合算出second数。这种方法时间复杂度只超过了23%

你可能感兴趣的:(【Leetcode】Set Mismatch)