zoukankan      html  css  js  c++  java
  • Java实现LeetCode_0027_RemoveElement

    package javaLeetCode.primary;
    
    import java.util.Scanner;
    
    public class RemoveElement_27 {
    	public static void main(String[] args) {
    		int[] nums= {0,1,2,2,3,0,4,2};
    		System.out.println("Please input a variable");
    		@SuppressWarnings("resource")
    		Scanner input = new Scanner(System.in);
    		int val = input.nextInt();
    		System.out.println(removeElement_2(nums,val));
    	}// end main()
    
    	/**
    	 * 
    	 * */
    	/*
    	 * Test Data:
    	 * [3,2,2,3]--2--2
    	 * [0,1,2,2,3,0,4,2]--2--5
    	 * 
    	 */
    	public static int removeElement_1(int[] nums, int val) {
    		int j=0;
    		int i=0;
    		while(i<nums.length)
    		{
    			if(nums[i]!=val)
    				nums[j++]=nums[i++];
    			else
    				i++;
    		}//end while
    //		for(i=0;i<j;i++) {
    //			System.out.print(nums[i]);
    //		}//end for
    //		System.out.println();
    		return j;
    	}// end removeElement()
    	
    	/**
    	 * Answer online.
    	 * */
    	public static int removeElement_2(int[] nums, int val) {
    	    int i = 0;
    	    for (int j = 0; j < nums.length; j++) {
    	        if (nums[j] != val) {
    	            nums[i] = nums[j];
    	            i++;
    	        }
    	    }
    	    return i;
    	}//end removeElement
    	
    	/**
    	 * Answer online.
    	 * */
    	public static int removeElement_3(int[] nums, int val) {
    	    int i = 0;
    	    int n = nums.length;
    	    while (i < n) {
    	        if (nums[i] == val) {
    	            nums[i] = nums[n - 1];
    	            // reduce array size by one
    	            n--;
    	        } else {
    	            i++;
    	        }//end if
    	    }//end while
    	    return n;
    	}//end removeElement()
    }// end RemoveElement_27
    
    
    
  • 相关阅读:
    《驱动学习 —— GPIO子系统和pinctl子系统》
    《驱动学习 —— input子系统》
    《视频相关名词了解》
    《网络编程 —— socket编程实例》
    uhci ohci ehci的区别
    phy的概念
    USB DEVICE
    gdb常用命令
    总线设备模型中注册
    module_i2c_driver
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947033.html
Copyright © 2011-2022 走看看