LeetCode 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.
题目给了我们一个nums array,nums 包含 1 到 n,其中有一个重复的,让我们找到重复的数字,和另外一个丢失的数字。
首先我们可以用公式 (1 + n) * n / 2 知道 nums 的总和 corSum。
接着遍历nums:
用HashSet 来找到重复的那个数字,存入res[0];
把所有数字累加sum,除了重复的那个数字。
最后 丢失的数字 = corSum - sum。
Java Solution:
Runtime beats 30.43%
完成日期:11/15/2017
关键词:HashMap, Math
关键点:求sum 公式
class Solution
{
public int[] findErrorNums(int[] nums)
{
HashSet<Integer> 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 res;
}
}
参考资料:n/a
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 645. Set Mismatch (集合不匹配)的更多相关文章
- Java实现 LeetCode 645 错误的集合(暴力)
645. 错误的集合 集合 S 包含从1到 n 的整数.不幸的是,因为数据错误,导致集合里面某一个元素复制了成了集合里面的另外一个元素的值,导致集合丢失了一个整数并且有一个元素重复. 给定一个数组 n ...
- leetcode 645. 错误的集合
问题描述 集合 S 包含从1到 n 的整数.不幸的是,因为数据错误,导致集合里面某一个元素复制了成了集合里面的另外一个元素的值,导致集合丢失了一个整数并且有一个元素重复. 给定一个数组 nums 代表 ...
- leetcode 645. Set Mismatch——凡是要节约空间的题目 都在输入数据上下功夫 不要担心破坏原始的input
The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...
- 645. Set Mismatch - LeetCode
Question 645. Set Mismatch Solution 思路: 遍历每个数字,然后将其应该出现的位置上的数字变为其相反数,这样如果我们再变为其相反数之前已经成负数了,说明该数字是重复数 ...
- Leetcode 之 Set Mismatch
645. Set Mismatch 1.Problem The set S originally contains numbers from 1 to n. But unfortunately, du ...
- 448. Find All Numbers Disappeared in an Array&&645. Set Mismatch
题目: 448. Find All Numbers Disappeared in an Array Given an array of integers where 1 ≤ a[i] ≤ n (n = ...
- 【Leetcode_easy】645. Set Mismatch
problem 645. Set Mismatch 题意: solution1: 统计每个数字出现的次数了,然后再遍历次数数组,如果某个数字出现了两次就是重复数,如果出现了0次,就是缺失数. 注意:如 ...
- [LeetCode] Set Mismatch 设置不匹配
The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...
- 【LeetCode】645. Set Mismatch 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Hash方法 直接计算 日期 题目地址: https ...
随机推荐
- GPC:使用GPC计算intersection容易出现的问题
在使用GPC计算多边形的交的时候,出现问题 //1.2. 另一种方法,判断新的多边形是否和老多边形相交 Poly cross = (PolyDefault) Clip.intersection ...
- CAD如何设置系统变量
主要用到函数说明: MxDrawXCustomFunction::Mx_SetSysVar 设置系统变量.详细说明如下: 参数 说明 CString sVarName 系统变量名 Value 需要设置 ...
- js实现字符串反转
方案1: var str = "abcdef"; console.log( str.split("").reverse().join("") ...
- Java运算符法则
JAVA运算符法则 运算符是一种特殊的符号,用于表示数据的运算,赋值和比较等: 算术运算符 正号+,负号-,加+,减-,乘*,除/,余或取模%,自增++,自减--,字符串相加+ 正号负号运算符代表运算 ...
- 查询条件中,不进sql语句 也不进后台bug
前端代码:本来代码中少写了value="1",后来加上value值之后,可以正常进方法 <div class="row"> <label cl ...
- TWaver HTML5之树形布局
转眼间春节假期已经过完,作为一个职业的程序猿,不知道大家有没有这样的感觉,一天不碰电脑,总觉得生活少点什么.今天是春节后上班的第三天,给大家分享一下我们前段时间的一个需求,需求是这样的:界面中的网元分 ...
- Win10中创建Hyper-V虚拟机
Win10虚拟机创建方法方法 1 开始菜单->所有应用->Windows系统->控制面板,程序->启用或关闭Windows功能,勾选Hyper-V下所有选项 如果Hyper-V ...
- 基于虚拟机的centos6.5 搭建本地光盘yum源
在线yum安装必须要保持服务器能够连入网络并且他下载的还会比较慢因为地址大部分多是国外的下载站.另外yum在线下载的都是比较新的软件包,可能不是很稳定,那么使用yum的本地资源就是光盘里的RPM包,让 ...
- Packages on Ubuntu OS
openSSH https://help.ubuntu.com/lts/serverguide/openssh-server.html
- 【Codeforces 600C】Make Palindrome
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 计算出来每个字母出现的次数. 把字典序大的奇数出现次数的字母换成字典序小的奇数出现次数的字母贪心即可. 注意只有一个字母的情况 然后贪心地把字 ...