LeetCode027 — Remove Element

題目大意: 給一個數組,指定一個值讓其移到後面去,其他的按順序移到前面,並返回移除指定值剩餘數的個數~

比較easy,遍歷一遍,等於指定的不走,不等於就賦值往後走~

public class Solution027 {

    public int removeElement(int[] nums, int val) {

        int index = 0;
        for(int i = 0; i < nums.length; i ++){
            if(nums[i] != val)
                nums[index++] = nums[i];
        }
        return index;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章