1. 문제 개요
문제 링크 : 27. Remove Element
Remove Element - LeetCode
Can you solve this real interview question? Remove Element - Given an integer array nums and an integer val, remove all occurrences of val in nums in-place [https://en.wikipedia.org/wiki/In-place_algorithm]. The order of the elements may be changed. Then r
leetcode.com
- 1개의 배열(nums)에서 주어지는 다른 1개의 변수(k)의 요소와 중복되는 값을 제거하시오
2. 문제 풀이
배열을 탐색하면서 변수와 중복되는 값을 제거하면 되는 문제
이를 구현하기 위해 주석으로 코드의 구조를 잡아보면 다음과 같다.
const removeElement = function(nums, val) {
// 배열 탐색 반복문
// 주어진 변수와 값이 같을경우 배열에서 제거
}
LeetCode 요구 양식으로 코드 구현
const removeElement = function(nums, val) {
for(let i = 0; i < nums.length; i++) {
if(nums[i] === val) {
nums.splice(i, 1);
i--;
}
}
return nums.length
};
LeetCode 제출 결과
3. 시간 복잡도 계산
더보기
O(n) = O(n)
4. 개선 사항
배열을 이용하여 Set을 만들다 보니 기존의 배열값임 수정되는 것이 아니라 메모리 사용량이 많졌고
이후 Set으로 만들어진 값을 기존 배열에 대입하다 보니 보정 과정이 필요하여 추가적으로 메모리를 더 사용한듯하여 이에 대한 개선이 필요합니다
5. 깃허브
관련 사항은 깃허브에 모두 기재되어 있습니다.
'LeetCode - Top Interview 150' 카테고리의 다른 글
[Java Script]LeetCode 189. Rotate Array (0) | 2023.08.24 |
---|---|
[Java Script]LeetCode 169. Majority Element (0) | 2023.08.24 |
[Java Script]LeetCode 80. Remove Duplicates from Sorted Array II (0) | 2023.08.24 |
[Java Script]LeetCode 26. Remove Duplicates from Sorted Array (0) | 2023.08.24 |
[Java Script]LeetCode 88번 Merge Sorted Array (0) | 2023.08.23 |