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:

    1. The given array size will in the range [2, 10000].
    2. The given array's numbers won't have any order.
 
有三种解法,(1)直接sum计算找出缺失值,需要用到set找到重复数。(2)使用sort思路。数字按照预定的规则排列。(3)将set的使用放在nums里,nums里的数字为负数表示该位置存在对应数字。
代码分别如下:
class Solution(object):
def findErrorNums(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
n = len(nums)
s = sum(set(nums))
return [sum(nums)-s, n*(n+1)/2-s]
 
class Solution(object):
def findErrorNums(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
# sort
for i,n in enumerate(nums):
while nums[i] != nums[nums[i]-1]:
t = nums[i]
nums[i] = nums[nums[i]-1]
nums[t-1] = t
for i,n in enumerate(nums):
if n != i+1:
return [n,i+1]
class Solution(object):
def findErrorNums(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
for i in xrange(len(nums)):
pos = nums[i] if nums[i]>0 else -nums[i]
if nums[pos-1] > 0:
nums[pos-1] = -nums[pos-1]
else:
dup = pos
for i, n in enumerate(nums):
if n > 0:
return [dup, i+1]

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

  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. 645. Set Mismatch - LeetCode

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

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

  4. Leetcode 之 Set Mismatch

    645. Set Mismatch 1.Problem The set S originally contains numbers from 1 to n. But unfortunately, du ...

  5. 【Leetcode_easy】645. Set Mismatch

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

  6. 【LeetCode】645. Set Mismatch 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Hash方法 直接计算 日期 题目地址: https ...

  7. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  8. __slots__节约空间

    1.为什么要使用__slots__ Python 使用 dicts(hash table)缓存大量的静态资源(属性). 我们最近在Image类中,用仅仅一行__slots__代码,改变成使用tuple ...

  9. 645. Set Mismatch挑出不匹配的元素和应该真正存在的元素

    [抄题]: he set S originally contains numbers from 1 to n. But unfortunately, due to the data error, on ...

随机推荐

  1. 前端基础 & 初识CSS

    CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素.l 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 每个CS ...

  2. python函数补充

    一 作用域 作用域介绍 python中的作用域分4种情况: L:local,局部作用域,即函数中定义的变量: E:enclosing,嵌套的父级函数的局部作用域,即包含此函数的上级函数的局      ...

  3. chrome浏览器自动填充失效问题

    现在浏览器带有自动填充,一般在input标签中增加autocomplete="off" 可以进行控制,off代表不填充,on代表填充,这个属性也可以放在form标签中,对所有的in ...

  4. (扫盲)WebSocket 教程

    原文地址:http://www.ruanyifeng.com/blog/2017/05/websocket.html WebSocket 是一种网络通信协议,很多高级功能都需要它. 本文介绍 WebS ...

  5. vimium的使用介绍和基本用法

    vimium是chrome浏览器的一个插件,fq去chrome应用商店搜索vimium,下载安装 纯键盘操作,脱离了鼠标,提高效率 核心是f,安装好vimium后只需要按f,输入对应的编号就能进入相应 ...

  6. jQuery:自学笔记(5)——Ajax

    jQuery:自学笔记(5)——Ajax 使用Ajax快捷函数 说明 出于简化AJAX开发工作的流程,jQuery提供了若干了快捷函数. 实例 1.显示 test.php 返回值(HTML 或 XML ...

  7. UI控件之UIScrollView

    UIScrollView:提供了滚动功能,用来显示超过一屏的视图 创建滚动视图 UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame: ...

  8. bex5部署后不更新

    哪个模块没更新,就编译哪个模块 在x5/tools/compile下,运行对应模块的bat,并清空浏览器缓存 如果修改了.w文件,也可以删除相应的.catch文件夹 和.release文件夹,并且注意 ...

  9. 3D立方体图片切换动画

    在线演示 本地下载

  10. 九、goroutine和channel

    进程和线程 A)进程是程序在操作系统中的一次执行过程,系统进行资源分配和调度的一个独立单元 B)线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位 C)一个进 ...