您的位置:首页 > 编程语言 > Java开发

leetcode:Remove Element 【Java】

2016-03-06 11:28 459 查看
一、问题描述

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.
二、问题分析

添加一个下标指针即可。

三、算法代码

public class Solution {
    public int removeElement(int[] nums, int val) {
        int index = -1;
        for(int i = 0; i <= nums.length - 1; i++){
            if(nums[i] != val){
                nums[++index] = nums[i];
            }
        }
        return index + 1;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: