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;
        }
        
    }


  • 相关阅读:
    Python3基础 函数 未指定返回值,返回NONE
    Python3基础 函数 有参数有返回值 对传入的参数加1
    Python3基础 函数 无参数无返回值 调用会输出hello world的函数
    Python3基础 函数 收集参数(tuple)+普通参数 的示例
    MVC中几种常用ActionResult
    sqlserver 中存储过程的基础知识记录
    常用的正则表达式方法2
    常用的正则表达式方法1
    vs2012运行项目报未能加载文件或程序集“System.Web.Mvc, Version=4.0.0.1,Culture=neutral”问题和解决方法
    怎样解决PowerDesigner15出现许可证过期问题?
  • 原文地址:https://www.cnblogs.com/Moriarty-cx/p/9751025.html
Copyright © 2011-2022 走看看