zoukankan      html  css  js  c++  java
  • [array] leetCode-27. Remove Element

    27. Remove Element - Easy

    descrition

    Given an array and a value, 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

    Given nums = [3,2,2,3], val = 3,
    
    Your function should return length = 2, with the first two elements of nums being 2.
    

    解析

    核心思想:双指针;注意需求的分析,根据需求进一步优化设计。

    方法 1

    参见 code: int removeElementKeepOrder(vector

    快慢指针,icur 指向新数组的结尾,i 遍历数组,只要当前值不等于 val 则进行复制。

    方法 2

    注意到题目的两个前提条件:(1)数组中元素的位置可以改变;(2)超过最新长度 length 后面的元素无关紧要。

    这两个条件使得算法进一步优化成为可能。比如当 nums=[1,2,3,5,4],val=4,如果使用方法 1,将产生 4 次赋值操作。而 nums=[4,1,2,3,5],val=4 时,也将产生 4 次赋值操作,实际上我们可以直接将 4 直接删掉的。

    还是两个指针,只不过这是两个对立的指针,icur 从前往后,指向当前要检查的数,在此之前的数都是满足要求的,当 nums[icur] 不满足要求时,值需要将 nums[iend] 赋值给它,并将 iend 自减即可, 这相当于之间删除 val。虽然时间复杂度还是 O(n),但实际上赋值操作的次数只等于需要删除的元素个数,当需要删除的元素个数很少时,算法很高效。

    code

    
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    class Solution{
    public:
    	int removeElement(vector<int>& nums, int val){
    		//return removeElementKeepOrder(nums, val);
    		return removeElementChangePlace(nums, val);
    	}
    
    	// time-O(n), space-O(1)
    	int removeElementKeepOrder(vector<int>& nums, int val){
    		int icur = 0;
    		for(int i=0; i<nums.size(); i++){
    			if(nums[i] != val){
    				nums[icur++] = nums[i];
    			}
    		}
    
    		return icur; // return new length
    	}
    
    	// time-O(n). In this approach, the number of assignment operation is 
    	// equal to the number of elements to remove. So it is more efficent if 
    	// elements to remove are rare.
    	// Note: this optimization is based on the condition of 
    	//     (1) The order of elements can be changed
    	//     (2) It doesn't matter what you leave beyond the new length.   
    	// space-O(1),
    	int removeElementChangePlace(vector<int>& nums, int val){
    		int icur = 0;
    		int iend = nums.size() - 1;
    		while(icur<=iend){
    			if(nums[icur] == val){
    				// move nums[icur] to the end and delete which operation is equal to subtract iend.
    				// note: icur can't be decrease, because the current element
    				// dosen't check 
    				nums[icur] = nums[iend];
    				iend--;
    			}else{
    				icur++;
    			}
    		}
    
    		return iend+1;
    	}
    };
    
    int main()
    {
    	return 0;
    }
    
    
  • 相关阅读:
    HDU 1301 Jungle Roads
    HDU 1671 Phone List
    HDU 1992 Tiling a Grid With Dominoes
    HDU 1251 统计难题
    总结自己的近期表现
    Windows API 函数: SetClassLong
    ModifyStyle
    assert,assert_valid,verify,trace用法
    用VC++绘制位图按钮
    Codeforces 144D. Missile Silos 最短路
  • 原文地址:https://www.cnblogs.com/fanling999/p/7829482.html
Copyright © 2011-2022 走看看