[抄题]:

he 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]

[暴力解法]:

hashset

时间分析:

空间分析:n

[优化后]:

时间分析:

空间分析:1

[奇葩输出条件]:

[奇葩corner case]:

  1. 把数字1对应到nums[],为了不出现溢出,需要-1

[思维问题]:

[一句话思路]:

为了优化空间,判断重复性时 直接*(-1)

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 循环中先写退出条件,再写正常条件

[二刷]:

  1. 标记元素为负数之后,记得需要变回正数才能正常返回

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

为了优化空间,判断重复性时 直接*(-1)

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

//for loop, * (-1), res[0]
for (int num : nums) {
if (nums[Math.abs(num) - 1] < 0) {
res[0] = Math.abs(num);
}else {
nums[Math.abs(num) - 1] *= (-1);
}
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

287. Find the Duplicate Number 快慢指针可以找重复?不是找中点么

[代码风格] :

class Solution {
public int[] findErrorNums(int[] nums) {
//ini
int[] res = new int[2]; //cc
if (nums == null || nums.length == 0) {
return res;
} //for loop, * (-1), res[0]
for (int num : nums) {
if (nums[Math.abs(num) - 1] < 0) {
res[0] = Math.abs(num);
}else {
nums[Math.abs(num) - 1] *= (-1);
}
} //find res[1]
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
res[1] = i + 1;
}
} //return
return res;
}
}

645. Set Mismatch挑出不匹配的元素和应该真正存在的元素的更多相关文章

  1. LeetCode 645. Set Mismatch (集合不匹配)

    The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...

  2. 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 = ...

  3. 20190129-‘abcdefgh’里面挑出3个字母进行组合,一共有多少组合

    一. 百度面试题‘abcdefgh’里面挑出3个字母进行组合,一共有多少组合,要求3个字母中不能有重复的组合,三个字母同时出现的次数只能出现一次,如出现了abc就不能出现cab,bca等 思路: 1. ...

  4. 【Leetcode_easy】645. Set Mismatch

    problem 645. Set Mismatch 题意: solution1: 统计每个数字出现的次数了,然后再遍历次数数组,如果某个数字出现了两次就是重复数,如果出现了0次,就是缺失数. 注意:如 ...

  5. JS控制语句 编程练习 学生数据,分别是姓名、性别、年龄和年级,接下来呢,我们要利用JavaScript的知识挑出其中所有是大一的女生的的名字哦。

    编程练习 在一个大学的编程选修课班里,我们得到了一组参加该班级的学生数据,分别是姓名.性别.年龄和年级,接下来呢,我们要利用JavaScript的知识挑出其中所有是大一的女生的的名字哦. 学生信息如下 ...

  6. 645. Set Mismatch - LeetCode

    Question 645. Set Mismatch Solution 思路: 遍历每个数字,然后将其应该出现的位置上的数字变为其相反数,这样如果我们再变为其相反数之前已经成负数了,说明该数字是重复数 ...

  7. C语言数据结构链栈(创建、入栈、出栈、取栈顶元素、遍历链栈中的元素)

    /**创建链栈*创建一个top指针代表head指针*采用链式存储结构*采用头插法创建链表*操作 创建 出栈 入栈 取栈顶元素*创建数据域的结构体*创建数据域的名称指针*使用随机函数对数据域的编号进行赋 ...

  8. parents([expr]) 取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素)。可以通过一个可选的表达式进行筛选。

    parents([expr]) 概述 取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素).可以通过一个可选的表达式进行筛选.大理石平台检定规程   参数 exprStringV1.0 用于 ...

  9. leetcode 645. Set Mismatch——凡是要节约空间的题目 都在输入数据上下功夫 不要担心破坏原始的input

    The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...

随机推荐

  1. Python 调用C函数

    /******************************************************************** * Python 调用C函数 * 说明: * Tony在处理 ...

  2. python 中出现 “IndentationError: expected an indented block” 问题

    python 学习 在定义Python函数的时候如下 >>>def hello() . . .print "hello" 这样会报错的,报错如下: Indenta ...

  3. 你该了解的10个 Python 模块

    Python很优雅.使用以下模块有助于保持你的代码整洁.易于维护.欢迎补充. Docopt.忘了optparse和argparse吧,使用docstring来构建优雅的.高可读性.复杂(如果你有这个需 ...

  4. Biology(湖南集训)

    题目大意:n个字符串,m个操作,可以插入字符串,也可以询问某T个字符串的最长后缀 题解:Trie+lca Trie树的插入与查询操作.把字符串反转就相当于求公共前缀. lca的深度就是公共前缀的长度. ...

  5. python操作rabbitmq、redis

    1.启动rabbimq.mysql 在“”运行“”里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd,进入redis所在目录,执行redis- ...

  6. hadoop复合键排序使用方法

    在hadoop中处理复杂业务时,需要用到复合键,复合不同于单纯的继承Writable接口,而是继承了 WritableComparable<T>接口,而实际上,WritableCompar ...

  7. 关于ant及svnant的一点随记

    在使用svnant的时候: 注意一下: 1.JDK版本,svnant目前更新到1.3.1,其中svnkit.jar是不支持1.7/1.8JDK的,容易出现各种错误 Ps:下载http://www.sv ...

  8. ④SpringBoot之thymeleaf使用

    本文介绍SpringBoot使用的模板技术thymeleaf以及通过webJar进行前端资源的引入以及使用thymeleaf介绍简单说, Thymeleaf 是一个跟 Velocity.FreeMar ...

  9. phpcms文档

    http://www.phpcms.cn/doc/PHPCMSDocumentor/cache_module.html http://www.cnblogs.com/Braveliu/p/507493 ...

  10. 文化之旅(dijstra)

    2012_p4 文化之旅 (culture.cpp/c/pas) 时间限制: 1 Sec  内存限制: 128 MB提交: 43  解决: 16[提交][状态][讨论版][命题人:外部导入] 题目描述 ...