Remove Element
-
[leetcode] 27. Remove Element알고리즘 2021. 11. 22. 21:23
Description 정수 배열과 특정 값이 주어졌을때, 배열 안에 특정 값이 있다면 모두 제거하는 문제 Input: arr = [3, 2, 2, 3], val = 3 Output: [2, 2, _, _] Input: arr = [0, 1, 2, 2, 3, 0, 4, 2], val = 2 Output: [0, 1, 4, 0, 3, _, _, _] Success 두 개의 포인터 변수를 이용하여 인덱스를 순회하고 특정 값이 아닌 값들을 앞에 배치시킨다. class Solution { public int removeElement(int[] nums, int val) { if (nums == null) return -1; int j=0; for (int i=0; i
-
[leetcode] 27. Remove Element알고리즘 2021. 11. 9. 22:53
Description 정수로 이루어진 nums 배열과 특정 값 val 이 매개변수로 주어졌을때 배열 안에 val 과 동일한 값이 있으면 해당 배열에서 제거한 후 배열안에 유효한 값이 몇개인지를 출력하는 문제 Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2,_,_] Input: nums = [0,1,2,2,3,0,4,2], val = 2 Output: 5, nums = [0,1,4,0,3,_,_,_] Success cnt와 i 라는 두 개의 변수를 두어 nums[i] 의 값이 val 과 같지 않으면 cnt 가 가르키는 인덱스로 값을 채워 나간다. 그렇게 값을 채워나가다 보면 cnt 에는 val 의 값에 해당되지 않는 유효한 값들만 존재하게되고 cnt 는 ..