leetcode Remove Element(easy) /java

和上一道题思路差不多。

import java.io.*;
import java.util.*;

public class Solution {
    public static int removeElement(int[] nums, int val) {
        int r=0;
        int len=nums.length;
        int i=0,j=0;
        if(len==0)
            return 0;

        int c=0;
        for(i=0;i<len;i++)
        {
            if(val!=nums[i])
            {
                nums[j]=nums[i];
                j++;
            }
            else
            {
                c++;
            }
        }
        r=len-c;
        return r;
    }
    public static void main(String[] args)
    {
        int[] a={1,1,1,1,4,5};
        System.out.println(removeElement(a,1));
        int[] b={1,1,4,4,5};
        System.out.println(removeElement(b,4));
    }
}

 

posted @ 2017-07-03 09:28  天气晚来秋  阅读(117)  评论(0编辑  收藏  举报