• 문제 링크
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};
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 657. Robot Return to Origin - Java (0) | 2026.09.18 |
|---|---|
| [LeetCode] 653. Two Sum IV - Input is a BST - Java (0) | 2026.09.17 |
| [LeetCode] 643. Maximum Average Subarray I - Java (0) | 2026.09.15 |
| [LeetCode] 637. Average of Levels in Binary Tree - Java (0) | 2026.09.14 |
| [LeetCode] 628. Maximum Product of Three Numbers - Java (0) | 2026.09.13 |
댓글