1ms Java Solution


  • 0
    P
    public class Solution {
        public int removeElement(int[] nums, int val) {
    	        int index=nums.length-1;
    	        int size=nums.length-1;
    	        int last=nums.length-1;
    	        for(int i=0;i<=size;i++){
    	            if (nums[i]==val){
    	                while(!(index<0)&&nums[index]==val){
    	                    nums[index]=0;
    	                    index--;
    	                    size--;
    	                }
    	                if(!(index<0)&&nums[index]!=val&& (last!=i)){
    	                    if(index<i){
    	                    	continue;
    	                    }
    	                    nums[i]=nums[index];
    	                    nums[index]=0;
    	                    index--;
    	                    size--;
    	                }
    	                if(index<0){
    	                    break;
    	                }
    	            }
    	        }
    	      return size+1;  
    	    }
    }

  • 0
    N

    public class Solution {
    public int removeElement(int[] nums, int val) {
    int i =-1;
    for(int k = 0;k<nums.length;k++){
    if(nums[k] != val){
    i++;
    if(k>i){
    nums[i] = nums[k];
    }
    }

        }
        return i+1;
    }
    

    }

    It can be so concise.


Log in to reply
 

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