题目:

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.

 class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> ans;
for (int i = ; i < (signed) nums.size(); i++) {
int index = abs(nums[i]) - ;
if (nums[index] > )
nums[index] *= -;
else
continue;
}
for (int i = ; i < (signed) nums.size(); i++)
if (nums[i] > )
ans.push_back(i + );
return ans;
}
};

645.

 class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
vector<int> ans;
for (int i = ; i < (signed) nums.size(); i++) {
int index = abs(nums[i]) - ;
if (nums[index] > ) {
nums[index] *= -;
} else {
ans.push_back(index + );
}
}
for (int i = ; i < (signed) nums.size(); i++) {
if (nums[i] > )
ans.push_back(i + );
}
return ans;
}
};

448. Find All Numbers Disappeared in an Array&&645. Set Mismatch的更多相关文章

  1. 【leetcode】448. Find All Numbers Disappeared in an Array

    problem 448. Find All Numbers Disappeared in an Array solution: class Solution { public: vector<i ...

  2. leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array

    后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...

  3. 448. Find All Numbers Disappeared in an Array【easy】

    448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...

  4. 448. Find All Numbers Disappeared in an Array@python

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

  5. 5. 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 ...

  6. 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 ...

  7. leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)

    题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...

  8. 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 a ...

  9. [LeetCode&Python] Problem 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 ...

随机推荐

  1. codeforces-2

    B.chocolates #include<iostream> #include<algorithm> #include<string.h> long long b ...

  2. C# 中List<T>与DataSet之间的转换

    p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...

  3. django的url反向解析

    目的:防止页面中url地址改变,其他与这个URL地址有关联的都要改,减少耦合度 使用:主要分为在html中和视图函数中的使用 HTML中的使用: 如果我们在项目的url文件中通过include导入了应 ...

  4. keytool 错误: java.lang.Exception: 密钥库文件不存在: keystore

    通过Android Studio编译器获取SHA1 第一步.打开Android Studio的Terminal工具 第二步.输入命令:keytool -v -list -keystore keysto ...

  5. arcgis for js 之 获取两点之间的距离

    换了新公司,接触新行业,半路出家,看着别人的代码,看着api慢慢理解. 需求如下:已知两点坐标求距离. 思路,没有,站在同事的肩膀上踩路子,给的这个链接 https://developers.arcg ...

  6. appium工作原理

    Appium原理 面试的时候,被问到appium原理,一点不会,实在尴尬.大家可以直接翻看原作https://blog.csdn.net/jffhy2017/article/details/69220 ...

  7. jsp页面在Android系统和ISO系统的兼容性问题

    问题:一个jsp页面在Android手机上显示正常,但到了ISO系统上jsp页面的样式不显示了. 原因:css文件中设置样式时单位不兼容. 解决方案:将rem 转换成px;

  8. 【mac微信小助手】WeChatPlugin使用教程!

    微信小助手 mac版集微信防撤回和微信多开等诸多功能于一身,可以有效的阻止朋友微信撤回消息,还能开启无手机验证登录,再也不用每次登录扫码验证啦,非常方便!   wechatplugin mac版安装教 ...

  9. MATLAB R2018b Mac中文版安装教程

    MATLAB r2018b mac中文版是一款强大的可视化数学分析软件,专门用于在Mac上执行数值计算,编程和可视化任务时极大地提高您的工作效率.在MATLAB的帮助下,您可以分析数据,创建应用程序, ...

  10. 创建局域网yum服务器

    首先需要安装createrepo这个软件包,使用yum安装即可 创建软件库文件夹,比如mkdir -p /var/www/html/myrepo/x86_64, 将软件包拷贝到文件夹下. cd到该文件 ...