zoukankan      html  css  js  c++  java
  • [LeetCode] 27. Remove Element


    Given an array nums and a value val, remove all instances of that value in-place and return the new length.

    Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

    The order of elements can be changed. It doesn't matter what you leave beyond the new length.

    Example 1:

    Given nums = [3,2,2,3], val = 3,
    
    Your function should return length = 2, with the first two elements of nums being 2.
    
    It doesn't matter what you leave beyond the returned length.
    

    Example 2:

    Given nums = [0,1,2,2,3,0,4,2], val = 2,
    
    Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
    
    Note that the order of those five elements can be arbitrary.
    
    It doesn't matter what values are set beyond the returned length.

    题意:从一个数组中删除与val相应的元素,并返回删除后的长度
    先理解这里的删除,其实就是都放到队尾
    解法一:双指针,头换尾,尾的值是val就是往前移动,
    class Solution {
        public int removeElement(int[] nums, int val) {
            int sum = 0;
            int i = 0;
            int j = nums.length - 1;
            while (i <= j) {
                if (i == j) {
                    if (nums[i] == val)
                        sum ++;
                    break;
                }
                if (nums[i] == val) {
                    while (nums[j] == val) {
                        j--;sum ++;
                        if (j < 0)
                            break;
                        if (j == i)
                            break;
                    }
                    if (j < 0) {
                        break;
                    }
                    swap(nums, i, j);
                    j --;sum++;
                } else {
                    i ++;
                }
            }
            return nums.length - sum;
        }
        private void swap(int[] arr, int i, int j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    解法二:边移动边换
    class Solution {
        public int removeElement(int[] nums, int val) {
            int j = 0;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] == val) {
                    nums[j] = nums[i];
                    j ++;
                }
            }
            return j;
        }
    }

    解法三:先排序,然后操作,因为排了序之后所有相同的val就都在一起了

    (其实这里我不是很能理解,为什么这个O(nlogn)的算法确快了,这个代码我是抄的,因为一直想着应该是O(n)的算法一开始就没想过排序操作 (笑)

    后来我又审查了我的代码,可能在n比较小的时候,可能由于交换需要的代价比较大吧,)

    class Solution {
        public int removeElement(int[] nums, int val) {
            
            Arrays.sort(nums);
            
            int result=0;//未与k重复元素的下标
                
            for(int i=0;i<nums.length;i++){
    
                if(nums[i]==val){
                    
                    //在数组中i之后找到第一个与k不相等的元素的下标
                    i=noEqualsK(nums,i+1,val);
                    for(;i<nums.length;i++){
                        
                        nums[result++]=nums[i];
                        
                    }
                    
                }else{
                    
                    result++;
                    
                }
            }
            
            return result;
    
        }
        //返回数组中i之后找到第一个与k不相等的元素的下标
        public int noEqualsK(int[] nums,int i,int val){
            
            for(;i<nums.length;i++){
    
                if(nums[i]!=val){
                    
                    return i;
                    
                }
            }
            
            return nums.length;
        }
        
    }


  • 相关阅读:
    将动态aspx页面转换成为静态html页面的几种方法
    IHttpHandler 概述
    .Net,你究竟有多慢
    Js离开提示
    vs2008Webconfig文件
    什么是URL转发和一个IP建多个Web站点主机头名法
    sqlserver 统计sql语句大全收藏
    web.config 读写操作
    httpModules 与 httpHandlers
    简单的介绍一下常用的正规表示式:
  • 原文地址:https://www.cnblogs.com/Moriarty-cx/p/9751025.html
Copyright © 2011-2022 走看看