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 (nums[i-1] < 0) a = nums[i-1] * -1;
            nums[i-1] *= -1;
        }

        int b = 0;
        for (int i : nums) {
            if (nums[i-1] > 0 && nums[i-1] != a) {
                b = nums[i-1];
                break;
            }
        }
        return new int[]{a, b};
        */
    int[] res = new int[2];
    for (int i : nums) {
        if (nums[Math.abs(i) - 1] < 0) res[0] = Math.abs(i);
        else nums[Math.abs(i) - 1] *= -1;
    }
    for (int i=0;i<nums.length;i++) {
        if (nums[i] > 0) res[1] = i+1;
    }
    return res;
}

原文地址:https://www.cnblogs.com/okokabcd/p/9215859.html

时间: 2024-10-09 20:45:43

645. Set Mismatch - LeetCode的相关文章

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 数组缺少的数字

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

(Easy) Set Mismatch -LeetCode

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 in repetition of one number and loss of another number.

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 645. 错误的集合(Set Mismatch)

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

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