https://leetcode.com/problems/remove-element/description
Q. 정수배열 nums와 val 이 주어질 때, val을 제외한 nums배열을 다시 nums배열에 출력하고 그 길이를 반환하라.
이때 추가 메모리를 사용하지 말아라.
Solution.
어디서 본 0 뒤로 밀기와 비슷한 문제.
그냥 대상이 아닌 값들을 제외한 값들을 당긴다.
public int RemoveElement(int[] nums, int val)
{
int idx = 0;
for(int q = 0; q < nums.Length; ++q)
{
if(nums[q] != val)
nums[idx++] = nums[q];
}
return idx;
}
빠른 코드와 별 차이가 없는데, 왜 느린속도로 나오는지 잘... ;;;
'Leetcode > Top Interview 150' 카테고리의 다른 글
[Array/String][Medium] 80. Remove Duplicates from Sorted Array II (0) | 2024.04.28 |
---|---|
[Array/String][Easy] 26. Remove Duplicates from Sorted Array (1) | 2024.04.28 |
[Array/String][Easy] 88. Merge Sorted Array (0) | 2024.04.28 |
Contains Duplicate (0) | 2021.06.02 |
Array - Rotate Array (0) | 2021.05.27 |