[leetcode]

wave·2021년 3월 9일
0

매일 알고리즘

목록 보기
5/5

Set Mismatch

You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.

You are given an integer array nums representing the data status of this set after the error.

Find the number that occurs twice and the number that is missing and return them in the form of an array.

첫번째 코드

var findErrorNums = function (nums) {
    let myNums = [];
    for (let i = 0; i < nums.length; i++) {

        if (i > 0 && nums[i] === nums[i - 1]) {
            console.log(nums[i], nums.length);
            if (nums[i] == nums.length) {
                myNums.push(nums[i] - 1, nums[i]);
                return myNums;
            } else if (nums[i] < nums.length) {
                if (nums[i] == nums.length - 1) {
                    myNums.push(nums[i], nums[i] + 1);
                } else {
                    myNums.push(nums[i], nums[i] - 1);
                }
                return myNums;
            }
            myNums.push(nums[i], nums[i] + 1);
            return myNums;
        }
    }

};

// const nums = [1, 2, 2, 4];
// const nums = [1, 1];
// const nums = [2, 2];
const nums = [3, 2, 2];

const value = findErrorNums(nums);

console.log(value);

두번째 코드

var findErrorNums = function (nums) {
    let myNums = [];
    sortNums = [...new Set(nums)];
    for (let i = 0; i < nums.length; i++) {
        if (i > 0 && nums[i - 1] == nums[i]) {
            console.log(nums[i], nums.length);
            if (nums[i] < nums.length) {
                myNums.push(nums[i], nums[i] + 1);
            } else {
                myNums.push(nums[i] - 1, nums[i]);
            }
            return myNums;
        }
    }

};

// const nums = [1, 2, 2, 4];
// const nums = [1, 1];
// const nums = [2, 2];
const nums = [3, 2, 2];

const value = findErrorNums(nums);

console.log(value);

세번째 코드

var findErrorNums = function (nums) {
    let myNums = [];
    let min = Math.min(...nums);
    for (let i = 0; i < nums.length; i++) {
        if (i > 0 && nums[i - 1] == nums[i]) {

            // console.log(nums[i], nums.length);

            if (nums[0] <= min) {
                if (nums[i] > nums.length) {
                    myNums.push(nums[i] - 1, nums[i]);
                } else if (nums[i] == nums.length) {
                    myNums.push(nums[i], nums[i] - 1);
                } else {
                    myNums.push(nums[i], nums[i] + 1);
                }
            } else {
                myNums.push(nums[i], nums[i] - 1);
            }
            return myNums;
        }
    }
};

//23
//12
//21
//21
// const value1 = findErrorNums([1, 2, 2, 4]);
// const value2 = findErrorNums([1, 1]);
// const value3 = findErrorNums([2, 2]);
// const value4 = findErrorNums([3, 2, 2]);
const value5 = findErrorNums([2, 3, 2]);
// console.log(value1);
// console.log(value2);
// console.log(value3);
// console.log(value4);
console.log(value5);
profile
훌라도 하고 개발도 하는 사람

0개의 댓글

관련 채용 정보