Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- traversal
- stack
- Medium
- binary search tree
- Bubble Sort
- 카카오
- 트리
- Easy
- Bit
- sorting
- Subset
- 정렬
- Math
- LinkedList
- list
- 완전검색
- Linked List
- 배열
- 스택
- kakao
- 리스트
- leetcode
- bst
- 공채
- 2019 카카오
- tree
- 오픈 채팅방
- 순회
Archives
- Today
- Total
미리수얌 블로그
Leetcode: Set Mismatch 본문
Set Mismatch
주어진 배열은 원래 1 부터 n 까지 있어야 하지만 오류로 하나의 숫자가 1 부터 n 의 숫자로 변형 되었다.
중복된수와 변형된기 전의 숫자를 배열로 리턴하여라
예: [1, 2, 2, 4] 원래는 [1, 2, 3, 4] 였지만 3이 2로 변형됨. 따라서 return [2, 3]
예: [4, 3, 2, 3] 원래는 1 부터 4 여야 하지만 1이 3으로 변형됨. 따라서 return [3, 1]
이 문제는 푸는 방법이 여러개 입니다. 쉬운 방법은 정렬이겠죠. 저는 배열을 사용해서 해보겠습니다.
public int[] findErrorNums(int[] nums) {
int k = nums.length;
int sum = (k * (k + 1)) / 2;
int[] set = new int[k + 1];
int dupNum = -1;
for (int n: nums) {
sum -= n;
if (set[n] != 0) dupNum = n;
set[n] = n;
}
return new int[]{dupNum, dupNum + sum};
}
'코딩문제풀이 > Leetcode' 카테고리의 다른 글
Leetcode: Power of Four (0) | 2018.10.05 |
---|---|
Leetcode: Power of Two (0) | 2018.10.05 |
Leetcode: Diameter of Binary Tree (0) | 2018.09.29 |
Leetcode: String Compression (0) | 2018.09.29 |
Leetcode: Assign Cookies (0) | 2018.09.29 |
Comments