leetCode 27.Remove Element (删除元素) 解题思路和方法

Remove Element

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

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

思路:此题和26题一脉相承,算法上不难,具体如代码所示:

public class Solution {
    public int removeElement(int[] nums, int val) {
        int len = nums.length;
        int tempLen = len;
        int step = 0;//每个元素需要向前转移的距离
        for(int i = 0; i < len; i++){
            if(nums[i] == val){
                step++;//若相等步长+1
                tempLen--;//每一个相等的元素长度减少1
            }else{
                nums[i-step] = nums[i];//元素前移n个步长
            }
        }
        return tempLen;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-19 02:33:41

leetCode 27.Remove Element (删除元素) 解题思路和方法的相关文章

LeetCode 27.Remove Element 数组元素删除

27. Remove Element 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 in place with constant memory. The order of elements can be changed.

lintcode 容易题:Remove Element 删除元素

题目: 删除元素 给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响.  样例 给出一个数组 [0,4,4,0,0,2,4,4],和值 4 返回 4 并且4个元素的新数组为[0,0,0,2] 解题: Java程序: public class Solution { /** *@param A: A list of integers *@param elem: An integer *@return: The new length aft

[Leetcode] remove element 删除元素

Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 题意:删除给定的数,然后返回新的长度. 思路:这题的思路和sort colors差不多,是其简化版.大致的思路是:

LeetCode 27 Remove Element(移除元素)

翻译 给定一个数组和一个值,删除该值的所有实例,并返回新的长度. 元素的顺序可以被改变,也不关心最终的数组长度. 原文 Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new lengt

LeetCode Remove Element删除元素

1 class Solution { 2 public: 3 int removeElement(int A[], int n, int elem) { 4 int *p=A,*e=&A[n-1]; 5 int i,num=n; 6 for(i=0;i<n;i++){ //一共要对比n次,不能用n来处理,会影响循环 7 if(*p==elem){ 8 if(p==e) //已经处理到最后一个相同,只需总数减1 9 num--; 10 else{ 11 *p=*e; 12 e--; 13 nu

LeetCode#27 | Remove Element 移除元素

一.题目 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 element

LeetCode 27 Remove Element (C,C++,Java,Python)

Problem: Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. Solution: 和26题一样,就是判断条件不一样而已. 题目大意: 给一个数组,要求返回删除

leetcode 27 Remove Element

Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 题解:水题.遇到val就和最后一个不是val的数交换位置就好了. class Solution { public:

Leetcode 27 Remove Element STL

和remove zero类似的方法完成该题 1 class Solution { 2 public: 3 int removeElement(vector<int>& nums, int val) { 4 vector<int>::size_type j = 0; 5 for(vector<int>::size_type i = 0; i < nums.size(); ++i){ 6 if(nums[i] != val) nums[j++] = nums[