645. Set Mismatch

网友投稿 245 2022-09-17

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 { public int[] findErrorNums(int[] nums) { HashSet set = new HashSet<>(); int[] res = new int[2]; int corSum = (1 + nums.length) * nums.length / 2; int sum = 0; for(int num: nums) { if(set.contains(num)) res[0] = num; else { set.add(num); sum += num; } } res[1] = corSum - sum; return

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:DoMarketing-营销智库:郑爽“翻车”Prada遭连累,明星对高奢品牌的影响有多大?
下一篇:331. Verify Preorder Serialization of a Binary Tree
相关文章

 发表评论

暂时没有评论,来抢沙发吧~