Math-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:
vector<int> findErrorNums(vector<int>& nums) {
vector <int> ans;
for (int i = ; i < nums.size(); i++) {
int index = abs(nums[i]) - ;
if (nums[index] > ) {
nums[index] *= -;
}
else {
ans.push_back(index + );
}
}
for (int i = ; i < nums.size(); i++) {
if (nums[i] > ) {
ans.push_back(i + );
}
}
return ans;
}
};
求集合s中重复出现的数字、缺失的数字
Math-645. Set Mismatch的更多相关文章
- 645. Set Mismatch - LeetCode
Question 645. Set Mismatch Solution 思路: 遍历每个数字,然后将其应该出现的位置上的数字变为其相反数,这样如果我们再变为其相反数之前已经成负数了,说明该数字是重复数 ...
- 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次,就是缺失数. 注意:如 ...
- 645. Set Mismatch挑出不匹配的元素和应该真正存在的元素
[抄题]: he set S originally contains numbers from 1 to n. But unfortunately, due to the data error, on ...
- LeetCode 645. Set Mismatch (集合不匹配)
The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...
- 645. Set Mismatch
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- leetcode 645. Set Mismatch——凡是要节约空间的题目 都在输入数据上下功夫 不要担心破坏原始的input
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 ...
- Leetcode 之 Set Mismatch
645. Set Mismatch 1.Problem The set S originally contains numbers from 1 to n. But unfortunately, du ...
- leetcode算法总结
算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...
随机推荐
- vue2.0 element学习
1,bootstrap和vue2.0结合使用 vue文件搭建好后,引入jquery和bootstrap 我采用的方式为外部引用 在main.js内部直接导入 用vue-cli直接安装jquery和bo ...
- Golang之(for)用法
地鼠每次选好了一块地,打洞,坚持半个月发现地下有块石头,然后他就想绕路了...殊不知绕路只会让它离成果越来越远 package main import ( "fmt" " ...
- oracle使用 merge 更新或插入数据
OracleCC++C# 总结下.使用merge比传统的先判断再选择插入或更新快很多. 1)主要功能 提供有条件地更新和插入数据到数据库表中 如果该行存在,执行一个UPDATE操作,如果是一个新行, ...
- 解压查看二进制rpm包的方法
详细参考: man cpio 具体方法: rpm2cpio qt5-qtbase-5.6.0-13.fc21.x86_64.rpm | cpio -dium 执行后可在当前目录查看 安装目录 etc ...
- 20172325 2017-2018-2 《Java程序设计》第六周学习总结
20172325 2017-2018-2 <Java程序设计>第六周学习总结 教材学习内容总结 1.利用[ ]建立一个数组,整列数据可以通过数组名引用,数组中的每个元素则可以通过其在数组中 ...
- winobj
查看系统对象 windbg+pdb符号信息 查询各个系统的内核对象结构
- linux 静态链接库demo
目录结构 ./main.c #include<stdio.h> #include "./lib/jtlib1.h" int main() { pr ...
- 构建ASP.NET网站十大必备工具
最近使用ASP.NET为公司构建了一个简单的公共网站(该网站的地址:http://superexpert.com/).在这个过程中,我们使用了数量很多的免费工具,如果把构建ASP.NET网站的必备工具 ...
- sigint sigterm 有什么区别啊
SIGHUP 终止进程 终端线路挂断SIGINT 终止进程 中断进程SIGQUIT 建立CORE文件终止进程,并且生成core文件SIGILL 建立CORE文件 ...
- myeclipse svn 插件去除已经保存的密码方法
myeclipse svn 插件去除已经保存的密码方法 删除掉C:\Documents and Settings\hao\Application Data\Subversion\auth\svn. ...