645. Set Mismatch - LeetCode
Question

Solution
思路:
遍历每个数字,然后将其应该出现的位置上的数字变为其相反数,这样如果我们再变为其相反数之前已经成负数了,说明该数字是重复数,将其将入结果res中,然后再遍历原数组,如果某个位置上的数字为正数,说明该位置对应的数字没有出现过,加入res中即可
Java实现:
public int[] findErrorNums(int[] nums) {
/*
int a = 0;
for (int i : nums) {
if (nums[i-1] < 0) a = nums[i-1] * -1;
nums[i-1] *= -1;
}
int b = 0;
for (int i : nums) {
if (nums[i-1] > 0 && nums[i-1] != a) {
b = nums[i-1];
break;
}
}
return new int[]{a, b};
*/
int[] res = new int[2];
for (int i : nums) {
if (nums[Math.abs(i) - 1] < 0) res[0] = Math.abs(i);
else nums[Math.abs(i) - 1] *= -1;
}
for (int i=0;i<nums.length;i++) {
if (nums[i] > 0) res[1] = i+1;
}
return res;
}
645. Set Mismatch - LeetCode的更多相关文章
- 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 645. 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 ...
- 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
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 645. Set Mismatch挑出不匹配的元素和应该真正存在的元素
[抄题]: he set S originally contains numbers from 1 to n. But unfortunately, due to the data error, on ...
- Leetcode 之 Set Mismatch
645. Set Mismatch 1.Problem The set S originally contains numbers from 1 to n. But unfortunately, du ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- 纯干货数学推导_傅里叶级数与傅里叶变换_Part3_周期为2L的函数展开
- Vue-router实现单页面应用在没有登录情况下,自动跳转到登录页面
这是我做前端一来的第一篇文章,都不知道该怎么开始了.那就直接奔主题吧.先讲讲这个功能的实现场景吧,我们小组使用vue全家桶实现了一个单页面应用,最初就考虑对登录状态做限制.比如登录后不能后退到登录页面 ...
- vue-baidu-map 进入页面自动定位的解决方案!
写在前面:我只是一个前端小白,文章中的提到可能会有不足之处,仅提供一个参考.若有不完善的地方,欢迎各位大佬指出!,希望对你有帮助! 好了,入正题.其实之前也被这问题困扰过,在网上也查了一番,没找到解决 ...
- 快速安装 kafka 集群
前言 最近因为工作原因,需要安装一个 kafka 集群,目前网络上有很多相关的教程,按着步骤来也能完成安装,只是这些教程都显得略微繁琐.因此,我写了这篇文章帮助大家快速完成 kafka 集群安装. ...
- java中this这个概念初学者非常难理解,请举例说明
4.this关键字(this key word) 继上一小节,(3.一个对象可能有多个参考)this是当中的一个参考!指向他自己. class MyTestDate { int year; ...
- java之String字符串根据指定字符转化为字符串数组
public static void main(String[] args){ String str="护肤,药品,其他"; String temp[]; temp=str.spl ...
- CentOS7 DHCP自动获取IP地址
# 设置auto自动获取IP[root@localhost ~]# nmcli c modify eth0 ipv4.method auto //etho0 网卡名称 # 重新启动网卡并重新加 ...
- pip导出项目依赖包名称及版本,再安装命令
A导出依赖 pip freeze >requirements.txt B导入安装依赖 pip install -r requirements.txt 使用下面的命令安装依赖能自动跳过安装错误的依 ...
- Vue3 + Echarts 5 绘制带有立体感流线中国地图,建议收藏
本文绘制的地图效果图如下: 一.Echarts 使用五部曲 1.下载并引入 echarts Echarts 已更新到了 5.0 版本,安装完记得检查下自己的版本是否是 5.0 . npm instal ...
- Go xmas2020 学习笔记 04、Strings
04-Strings.unicode.utf-8.类型描述符.go 字符串在内存中的存储. Strings. String structure. String functions. Practice