leetcode 27:移除元素

https://leetcode.cn/problems/remove-element/

class Solution {
    public int removeElement(int[] nums, int val) {
        int count=0;
        int[] index=new int[100];
        for(int i=0;i

等于快慢指针法

class Solution {
    public int removeElement(int[] nums, int val) {
        // 快慢指针
        int slowIndex = 0;
        for (int fastIndex = 0; fastIndex < nums.length; fastIndex++) {
            if (nums[fastIndex] != val) {
                nums[slowIndex] = nums[fastIndex];
                slowIndex++;
            }
        }
        return slowIndex;
    }
}

你可能感兴趣的:(leetcode,算法,数据结构)