본문 바로가기
Problem Solving/LeetCode

[LeetCode] 645. Set Mismatch - Java

by graycode 2026. 9. 16.

• 문제 링크

 

Set Mismatch - LeetCode

Can you solve this real interview question? 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 res

leetcode.com

 

• 풀이 코드

public class Solution {

    public int[] findErrorNums(int[] nums) {
        int d = -1, m = -1;
        for (int num : nums) {
            int v = Math.abs(num), i = v - 1;
            if (nums[i] < 0) d = v;
            else nums[i] = -nums[i];
        }

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] <= 0) continue;
            m = i + 1;
            break;
        }

        return new int[]{d, m};
    }

}

댓글