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]的對(duì)nums計(jì)數(shù),再遍歷nums中的數(shù),找出m[num]==2的num即是第一個(gè)數(shù)
2 得出原始list的sum,再得出error list的sum,通過(guò)和first數(shù)結(jié)合算出second數(shù)。這種方法時(shí)間復(fù)雜度只超過(guò)了23%