[leetcode-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.

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.

思路:

用一个flag标记nums 中的数字是否出现过。如果出现过说明重复了。

最后再扫描一遍flag数组,如果有false标记说明没有出现过。

vector<int> findErrorNums(vector<int>& nums)
    {
        vector<int> ret;
        vector<bool>flag(nums.size()+1,false);

        for (int i = 0; i < nums.size();i++)
        {
             if (flag[nums[i]] == true)ret.push_back(nums[i]);
             flag[nums[i]] = true;
        }
        for (int i = 1; i < flag.size(); i++)
        {
            if (flag[i] == false)ret.push_back(i);
        }
        return ret;
    }
时间: 2024-10-09 20:45:23

[leetcode-645-Set Mismatch]的相关文章

LeetCode 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 arr

645. Set Mismatch - LeetCode

Question 645.?Set Mismatch Solution 思路: 遍历每个数字,然后将其应该出现的位置上的数字变为其相反数,这样如果我们再变为其相反数之前已经成负数了,说明该数字是重复数,将其将入结果res中,然后再遍历原数组,如果某个位置上的数字为正数,说明该位置对应的数字没有出现过,加入res中即可 Java实现: public int[] findErrorNums(int[] nums) { /* int a = 0; for (int i : nums) { if (nu

leetcode 645. 错误的集合(Set Mismatch)

目录 题目描述: 示例 1: 解法: 题目描述: 集合 S 包含从1到 n 的整数.不幸的是,因为数据错误,导致集合里面某一个元素复制了成了集合里面的另外一个元素的值,导致集合丢失了一个整数并且有一个元素重复. 给定一个数组 nums 代表了集合 S 发生错误后的结果.你的任务是首先寻找到重复出现的整数,再找到丢失的整数,将它们以数组的形式返回. 示例 1: 输入: nums = [1,2,2,4] 输出: [2,3] 注意: 给定数组的长度范围是 [2, 10000]. 给定的数组是无序的.

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 arr

645. Set Mismatch

Problem statement 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 nu

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

[抄题]: he 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 a

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

LeetCode Set Mismatch

原题链接在这里:https://leetcode.com/problems/set-mismatch/description/ 题目: 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

(分类讨论, 情景模拟) lintcode 640. One Edit Distance, leetcode 388,intcode 645. 13. 12. 659. 660

找特殊情况,分类讨论:三种情况 1)两个字符串的长度之差 大于1 直接返回false: 2)长度之差等于1, 判断长的字符串删掉不一样的字符,剩余的字符串是否相同: 3)长度之差等于0,判断不相同的字符个数,若超过一个返回false. class Solution { public: /** * @param s: a string * @param t: a string * @return: true if they are both one edit distance apart or f