https://leetcode.com/contest/leetcode-weekly-contest-42/problems/set-mismatch/
(需要登录才能看到,所以转载下题目)
645. 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.
Example 1:
Input: nums = [1,2,2,4]
Output: [2,3]
Note:
The given array size will in the range [2, 10000]. The given array's numbers won't have any order.
下面是解法
class Solution(object):
def findErrorNums(self, nums):
t1 = sum(nums)
t2 = sum(set(nums))
l = len(nums)
return [t1 - t2, l * (l + 1) / 2 - t2]
排名第四 好像还是个学生
PS: 如果有兴趣和时间做题 这期第四题用正则表达式解 也挺有趣的 https://leetcode.com/contest/leetcode-weekly-contest-42/problems/replace-words/