• 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 = 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 };
  • 相关阅读:
    The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.
    线程安全思考
    微服务网关哪家强?一文看懂Zuul, Nginx, Spring Cloud, Linkerd性能差异
    从构建分布式秒杀系统聊聊分布式锁
    基于Redis实现延时队列服务
    Redis分布式锁的正确实现方式
    探究 Redis 4 的 stream 类型
    JAVA 异常分类与理解
    缓存穿透,缓存击穿,缓存雪崩解决方案分析
    分布式之数据库和缓存双写一致性方案解析(一)
  • 原文地址:https://www.cnblogs.com/sindy/p/8280402.html
Copyright © 2020-2023  润新知