题目:

448. Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

645. Set Mismatch

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.

思路:

448和645具有相似的思路。两道题的共同点在于:元素的大小均为 [1,n]。448要求找出未出现的数字,645要求找出出现了两次的数字和未出现的数字。

由于元素的下标为[0,n-1],则元素的大小减去1得到的即为某个元素的下标,因此可以利用元素大小与下标之间的关系寻找特殊的数字。

对于448,遍历整个数组,通过元素的大小减去1得到下标。若该下标对应的元素为正,则将其乘以-1,变为负数;若该下标对应的元素为负,证明该下标之前已经出现过了一次,不作处理。通过这一次的遍历,仍然为正的元素所对应的下标再加1即为未出现过的元素。

对于645,遍历整个数组,通过元素的大小减去1得到下标。若该下标对应的元素为正,则将其乘以-1,变为负数;若该下标对应的元素为负,证明该下标之前已经出现过了一次,将该下标+1加入result中。通过这一次的遍历,仍然为正的元素所对应的下标再加1即为未出现过的元素。

代码:

448.

 1 class Solution {
 2 public:
 3     vector<int> findDisappearedNumbers(vector<int>& nums) {
 4         vector<int> ans;
 5         for (int i = 0; i < (signed) nums.size(); i++) {
 6             int index = abs(nums[i]) - 1;
 7             if (nums[index] > 0)
 8                 nums[index] *= -1;
 9             else
10                 continue;
11         }
12         for (int i = 0; i < (signed) nums.size(); i++)
13             if (nums[i] > 0)
14                 ans.push_back(i + 1);
15         return ans;
16     }
17 };

645.

 1 class Solution {
 2 public:
 3     vector<int> findErrorNums(vector<int>& nums) {
 4         vector<int> ans;
 5         for (int i = 0; i < (signed) nums.size(); i++) {
 6             int index = abs(nums[i]) - 1;
 7             if (nums[index] > 0) {
 8                 nums[index] *= -1;
 9             } else {
10                 ans.push_back(index + 1);
11             }
12         }
13         for (int i = 0; i < (signed) nums.size(); i++) {
14             if (nums[i] > 0)
15                 ans.push_back(i + 1);
16         }
17         return ans;
18     }
19 };

转载于:https://www.cnblogs.com/sindy/p/8280402.html

448. Find All Numbers Disappeared in an Array645. Set Mismatch相关推荐

  1. LeetCode 448. Find All Numbers Disappeared in an Array 442. Find All Duplicates in an Array

    这两道题很有意思,由于元素为1~n,因此每个元素的值-1(映射到0~n-1)就可以直接当做下标.这样将 nums 中对应下标的元素 *-1 以i表示 index+1 这个元素出现过了,能节省存储的空间 ...

  2. Leetcode 448. Find All Numbers Disappeared in an Array

    Leetcode  448. Find All Numbers Disappeared in an Array Add to List Description Submission Solutions ...

  3. leetcode 448. Find All Numbers Disappeared in an Array

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  4. leetcode 448. Find All Numbers Disappeared in an Array | 448. 找到所有数组中消失的数字(原地,位运算)

    题目 https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 题解 遍历数组,将数组中每个数字 n 作为下标,将 ...

  5. 448. Find All Numbers Disappeared in an Array

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  6. 448. Find All Numbers Disappeared in an Array 寻找有界数组[1,n]中的缺失数

    [抄题]: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice ...

  7. [swift] LeetCode 448. Find All Numbers Disappeared in an Array

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  8. 448. Find All Numbers Disappeared in an Array(找到所有数组中消失的数字)

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  9. Find All Numbers Disappeared in an Array

    Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...

最新文章

  1. Windows Phone 7 开发之:工具栏
  2. 深入Java泛型(六):Bean强转原理实践
  3. 微软亚洲研究院开源分布式机器学习工具包
  4. Java 打印菱形星块
  5. java字面量 方法区_(一)java的内存模型
  6. 高级 Linux 命令精通指南(2)
  7. cxf javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)
  8. ICMP报文分析(转)
  9. dbms_stats包更新、导出、导入、锁定统计信息
  10. 有钱鹅!腾讯奖励万名员工每人一台16999元华为手机,员工“十动然鱼”
  11. 陶哲轩实分析 命题 7.3.4 (Cauchy 准则) 证明
  12. python关闭函数解释器_深入 Python 解释器源码,我终于搞明白了字符串驻留的原理!...
  13. 深度学习笔记_搭建一个简单网络(完整版)_手写数字识别MNIST
  14. java编写flash相册的制作软件,Flash电子相册制作工具(Amazing Flash Gallery Maker)
  15. 618大促:手机品牌“神仙打架”,高端市场“谁主沉浮”?
  16. JSP+Servlet+Mysqll银行柜员业务绩效考核系统的设计与实现(附论文)
  17. VC图片的半透明处理
  18. php.ini配置文件中文详细解释
  19. This is My frist Webo Happy!!!
  20. 华为 GRE实验(GRE隧道)

热门文章

  1. 一些常见的iOS面试问题,一眼就能看出 初级和高级工程师的区别
  2. 前端事件绑定知识点(面试常考)
  3. Windows8 游戏开发教程-二、关于资源和工具
  4. 无需深厚技术背景,也可以做好系统和应用维护管理
  5. DOS批处理高级教程精选(二)
  6. Page.ClientScript.RegisterArrayDeclaration
  7. 在ASP.NET中调用存储过程方法新解
  8. ASP.NET---- Microsoft .NET Pet Shop 3.x(-)
  9. windows linux cpu 抢占式 时间片_阿里技术专家谈:CPU飙高,系统性能问题如何排查?...
  10. 哈希表 哈希函数 时间_您需要了解的哈希函数