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:

  1. Input: nums = [1,2,2,4]
  2. 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里的数字为负数表示该位置存在对应数字。
代码分别如下:
  1. class Solution(object):
  2. def findErrorNums(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: List[int]
  6. """
  7. n = len(nums)
  8. s = sum(set(nums))
  9. return [sum(nums)-s, n*(n+1)/2-s]
 
  1. class Solution(object):
  2. def findErrorNums(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: List[int]
  6. """
  7. # sort
  8. for i,n in enumerate(nums):
  9. while nums[i] != nums[nums[i]-1]:
  10. t = nums[i]
  11. nums[i] = nums[nums[i]-1]
  12. nums[t-1] = t
  13. for i,n in enumerate(nums):
  14. if n != i+1:
  15. return [n,i+1]
  1. class Solution(object):
  2. def findErrorNums(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: List[int]
  6. """
  7. for i in xrange(len(nums)):
  8. pos = nums[i] if nums[i]>0 else -nums[i]
  9. if nums[pos-1] > 0:
  10. nums[pos-1] = -nums[pos-1]
  11. else:
  12. dup = pos
  13. for i, n in enumerate(nums):
  14. if n > 0:
  15. 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. matlab学习笔记之求解线性规划问题和二次型问题

    一.线性规划问题 已知目标函数和约束条件均为线性函数,求目标函数的最小值(最优值)问题. 1.求解方式:用linprog函数求解 2.linprog函数使用形式: x=linprog(f,A,b)  ...

  2. linux c编程:System V消息队列一

    消息队列可以认为是一个消息链表,System V 消息队列使用消息队列标识符标识.具有足 够特权的任何进程都可以往一个队列放置一个消息,具有足够特权的任何进程都可以从一个给定队列读出一个消息.在某个进 ...

  3. 微信小程序排行榜

    哪类微信小程序使用量最多?小程序是附属在微信上,微信小程序排行榜跟微信的用户属性有很大的关系,微信用户对新闻资讯.情感.养生表现出了极大的兴趣,所有我们从新闻资讯小程序.视频小程序.情感类微信小程序. ...

  4. Centos----本地yum源制作

    本地YUM源制作 1.   YUM相关概念 1.1.  什么是YUM YUM(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的S ...

  5. IDEA中文出现乱码解决

    转自:http://lcl088005.iteye.com/blog/2284696 我是个idea的忠实用户,新公司的项目都是用eclipse做的,通过svn拉下代码后发现,注释的内容里,中文内容都 ...

  6. 正则表达式test match exec search

    (1)((2))(3)   $1  是第一个括号 $2  是第二个括号 $3  是第二个括号中的括号 $4  是第三个括号     http://www.jb51.net/article/28007. ...

  7. SOA宣言和微服务特点

    如果从概念层来看,我更喜欢把SOA归为企业架构的范畴,从企业架构出发把业务分解为不同业务域的服务,关注系统间的服务互联互通的规范,并不关心如何实现.也就是说在企业架构上使用SOA支撑业务,而在方案架构 ...

  8. bootstrap table 复选框使用

    var columns = [ { field : 'checked', checkbox: true, align: 'center', valign: 'middle', formatter:fu ...

  9. JDK源码 - ArrayList (基于1.7)

    前言   推荐一位大牛的博客: https://blog.csdn.net/eson_15/article/details/51121833 我基本都是看的他的源码分析,刚开始如果直接看jdk源码可能 ...

  10. [APIO2013]机器人

    题目描述 VRI(Voltron 机器人学会)的工程师建造了 n 个机器人.任意两个兼容的机 器人站在同一个格子时可以合并为一个复合机器人. 我们把机器人用 1 至 n 编号(n ≤ 9).如果两个机 ...