1ms and concise Java solution


  • 0
    N
    public class Solution {
    public int removeElement(int[] nums, int val) {
        int i =-1;
        for(int k = 0;k<nums.length;k++){
            if(nums[k] != val){
                i++;
                if(k>i){
                    nums[i] = nums[k];
                }
            }
            
        }
        return i+1;
    }
    

    }


  • 0

    Good solution, but there is no way to compare k and i, k will greater than i anyway.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.