移除元素 (Remove Element)

 

思路:

// @Title: 移除元素 (Remove Element)
// @Author: qisiii
// @Date: 2024-09-06 17:48:23
// @Runtime: 0 ms
// @Memory: 41 MB
// @comment: 
// @flag: 
class Solution {
    public int removeElement(int[] nums, int val) {
        int start=0,cur=0;
        while(cur<nums.length){
            if(nums[cur]!=val){
                nums[start++]=nums[cur];
            }
            cur++;
        }
        return start;
    }
}

思路:

// @Title: 移除元素 (Remove Element)
// @Author: qisiii
// @Date: 2024-04-13 18:20:27
// @Runtime: 0 ms
// @Memory: 41 MB
// @comment: 
// @flag: 
class Solution {
    public int removeElement(int[] nums, int val) {
        int left=0,right=0;
        while(right<nums.length){
            if(nums[right]!=val){
                nums[left++]=nums[right];
            }
            right++;
        }
        return left;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18