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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- 解决联想笔记本 安装VM虚拟机后每次启动都会导致电脑蓝屏问题
现象描述: pc为联想笔记本 系统是微软家庭中文版,每次启动VM虚拟机都会出现蓝屏现象,出现错误代码system_service_exception 原因及解决方法: 对于Windows10家庭版 ...
- 9_状态观测器设计_Linear Observer Design_Matlab_Simulink建模(上)
- 打造专属自己的html5拼图小游戏
最近公司刚好有个活动是要做一版 html5的拼图小游戏,于是自己心血来潮,自己先实现了一把,也算是尝尝鲜了.下面就把大体的思路介绍一下,希望大家都可以做出一款属于自己的拼图小游戏,必须是更炫酷,更好玩 ...
- Java/C++实现解释器模式---机器人控制程序
某机器人控制程序包含一些简单的英文指令,其文法规则如下: expression ::= direction action distance | composite composite ::= expr ...
- swig模板引擎和ejs模板引擎
swig模板引擎的基本用法: 1. 变量 {{ name }} //name名前后必须要加空格,不加就会报错 2. 属性 {{ student.name }} 3. 模板继承 swig使用exten ...
- TINY语言采用递归下降分析法编写语法分析程序
目录 自顶向下分析方法 TINY文法 消左提左.构造first follow 基本思想 python构造源码 运行结果 参考来源:聊聊编译原理(二) - 语法分析 自顶向下分析方法 自顶向下分析方法: ...
- Python程序的流程
1 """ 2 python程序的流程 3 """ 4 # ------------- 分支结构---------------- 5 # i ...
- [源码解析] TensorFlow 分布式环境(8) --- 通信机制
[源码解析] TensorFlow 分布式环境(8) --- 通信机制 目录 [源码解析] TensorFlow 分布式环境(8) --- 通信机制 1. 机制 1.1 消息标识符 1.1.1 定义 ...
- 创建可以运行宿主机GPU的容器
1.安装NVIDIA Container Runtime apt-get参考https://blog.csdn.net/li_ellin/article/details/107180516 yum参考 ...
- Spring-JdbcTemplate(注入到spring容器)-02
1.导入spring-jdbc和spring-tx坐标 <dependency> <groupId>junit</groupId> <artifactId&g ...