• 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
    
    
    
  • 相关阅读:
    函数传参总结
    集合操作总结
    深浅拷贝总结
    三级列表展示
    文件操作总结
    vue-router之嵌套路由
    vue-router之动态路由
    Sublime编辑VUE实现代码高亮
    Windows系统下Vue开发环境搭建详解版
    C#调用快递鸟电子面单API实现批量打印电子面单功能
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947033.html
Copyright © 2020-2023  润新知