Java 1ms solution o(n)


  • 0
    R
    public int removeElement(int[] nums, int val) {
        if(nums==null || nums.length==0){
            return 0;
        }
        int curse = 0;
        for(int index=0; index<nums.length;index++){
            if(nums[index]==val){
                continue;
            }
            else{
                nums[curse++]=nums[index];
            }
        }
        return curse;
    }

  • 1
    S

    Simple Java Solution. AC But I Feel there are some problems。

    public int removeElement(int[] nums, int val) {
                int i=0;
        		for (int a:nums) {
        			if (a!=val) {
        				nums[i++]=a;
        			}
         		}
        		return i;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.