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


  • 相关阅读:
    Android pm命令用法
    SSH没有password安全日志
    使用reserve要再次避免不必要的分配
    找到最大的迭代次数串
    个人存储不同类型的对象有一些想法的碰撞检测
    [Android 4.4.3] 泛泰A870 Mokee4.4.3 20140610 RC2.0 通过刷第三版 by syhost
    zoj 3823 Excavator Contest(结构体)
    【Linux】CentOS系统
    Swift学习——Swift解释特定的基础(七)
    Android JNI开发提高篇
  • 原文地址:https://www.cnblogs.com/Moriarty-cx/p/9751025.html
Copyright © 2020-2023  润新知