LeetCode-27 Remove Element(水题-去掉元素)

来源:互联网 发布:mac 如何画时间轴 编辑:程序博客网 时间:2024/06/06 16:34

LeetCode-27 Remove Element(去掉元素-水题)

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[] A, int elem) {    int j = 0;    int i = 0;    for (i = 0; i < A.length; i++) {if (A[i] == elem) {continue;}else {A[j] = A[i];j++;}}    return j;    }}


Runtime: 243 ms

水题。
0 0
原创粉丝点击