小白筆記--------------------leetcode27. Remove Element

public class Solution {
    public int removeElement(int[] nums, int val) {
        int count = 0;
        int result = 0;
        int s = nums.length;
        int j = 0;
        for(int i = 0 ; i < nums.length;i++){
            if(nums[i] != val){
                nums[j] = nums[i];
                j++;
                
            }else{
                count++;
            }
            
        }
        result = s - count;
        
        return result;
    }
}
給數組重新賦值是關鍵。。
發佈了102 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章