No.27 Remove Element

No.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.

Tags: Array Two Pointers

移除数组中所有的给定数字
要求:原地移除,不能使用额外空间;返回新数组的长度;可以改变原始顺序

典型的两指针

 1 #include "stdafx.h"
 2 #include <map>
 3 #include <vector>
 4 #include <iostream>
 5 using namespace std;
 6
 7 class Solution
 8 {
 9 public:
10     int removeElement(vector<int> &nums, int val)
11     {//移除数组中所有的给定数字
12      //要求:原地移除,不能使用额外空间;返回新数组的长度;可以改变原始顺序
13         int size = nums.size();
14         int count = 0;//计数当前val出现的次数
15
16         for(int i=0; i<size; i++)
17         {
18             if(nums[i] == val)
19                 count++;
20             else
21             {
22                 if(count != 0)
23                     nums[i-count] = nums[i];
24             }
25         }
26         nums.erase(nums.begin()+size-count,nums.end());
27         return size-count;
28     }
29 };
30
31 int main()
32 {
33     Solution sol;
34     int data[] = {1,2,2,3,3,2,1,7,9,10};
35     vector<int> test(data,data+sizeof(data)/sizeof(int));
36     for(const auto &i : test)
37         cout << i << " ";
38     cout << endl;
39     cout<< boolalpha << sol.removeElement(test,2)<<endl;
40         for(const auto &i : test)
41         cout << i << " ";
42     cout << endl;
43 }
时间: 2024-10-10 08:22:35

No.27 Remove Element的相关文章

27. Remove Element【easy】

27. Remove Element[easy] 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 ch

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.

27. Remove Element(js)

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 memor

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

[Leetcode][Python]27: Remove Element

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 27: Remove Elementhttps://oj.leetcode.com/problems/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 b

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. AC代码: class Solution(object): def removeElement(self, num

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

这道题跟26题很类似,并且有官方的答案.看了官方的答案之后发现写得特别巧,自己做的题太少思路太窄.有意思的是我的算法的时间复杂度是O(N^2),官方的是O(N),我的实际运行时间还少了2ms. iven 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

27. Remove Element java solutions

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. It doesn't matter