https://leetcode.com/problems/remove-element/?envType=study-plan-v2&envId=top-interview-150
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
📌 - 처음 생각한 풀이
📌- 풀이
- for문을 돌면서 num[i] 가 val이 아닐때
- count의 값에 해당하는 위치에 num[i]를 넣어주기
🔥 - Java 코드
import java.util.*;
class Solution {
public int removeElement(int[] nums, int val) {
int count =0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != val) {
nums[count++] = nums[i];
}
}
return count;
}
}