본문으로 바로가기

[LeetCode/릿코드] 27. Remove Element

category [JAVA] LeetCode/Easy 2023. 1. 13. 16:16
728x90
반응형

문제

 

 

반응형

풀이

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
    public int removeElement(int[] nums, int val) {
       if(nums.length == 0){
           return 0;
       }
 
        int i = 0;
        int j = nums.length-1;
       while(i<j){
           if(nums[i] == val) {
               nums[i] = nums[j];
               j--;
           }else{
               i++;
           }
       }
       if(nums[i] == val) {
           return i;
       }
       return i+1;
    }
}
cs

 

728x90
반응형