1. 문제
LeetCode - The World's Leading Online Programming Learning Platform
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
2. 문제 설명
간단하게 정렬된 배열에서 중복된 요소를 제거 하는 문제 이다.
3. 소스코드
class Solution {
public int removeDuplicates(int[] nums) {
HashSet<Integer> sets = new HashSet<>();
int idx = 0;
for (int num : nums) {
if (sets.add(num)) {
nums[idx] = num;
idx++;
}
}
return idx;
}
}
4. 문제 해설
그 이전에 풀었던 27번 문제 에 HashSet구조를 넣어서 풀었다 HashSet은 중복된 요소를 허용하지 않기 때문에 HashSet에 요소를 추가하면서 삭제를 해주면 된다.
[leetCode 27번] Remove Element (JAVA) (tistory.com)
[leetCode 27번] Remove Element (JAVA)
1.문제 LeetCode - The World's Leading Online Programming Learning Platform LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get pre
ldh7728.tistory.com
'Algorithm' 카테고리의 다른 글
| [leetCode 169번] Majority Element (Java) (0) | 2023.08.24 |
|---|---|
| [leetCode 80번] Remove Duplicates from Sorted Array II (Java) (0) | 2023.08.24 |
| [leetCode 27번] Remove Element (JAVA) (0) | 2023.08.24 |
| [leetCode 88 번] Merge Sorted Array (Java) (0) | 2023.08.23 |
| 프로그래머스 이진 변환 반복하기 (DFS 파이썬 풀이) (0) | 2023.07.25 |