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
    
    
    
  • 相关阅读:
    Python-os
    Python-字典Dict
    Linux下使用Apache搭建Web网站服务器
    Linux中FTP安装与配置
    第16章 广域网
    第15章 IPv6
    第14章 思科无线技术
    第13章 网络地址转换NAT
    第12章 安全
    第11章 虚拟局域网
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947033.html
Copyright © 2011-2022 走看看