leetcode 27

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. It doesn‘t matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3]val = 3

Your function should return length = 2, with the first two elements of nums being 2.

去除数列中指定的数,返回剩余数的个数n,并将剩余的数存入原数列的前n位。

代码如下:

 1 class Solution {
 2 public:
 3     int removeElement(vector<int>& nums, int val) {
 4         if(nums.empty())
 5         {
 6             return 0;
 7         }
 8         int n = nums.size();
 9         if(n == 1 && nums[0] == val)
10         {
11             return 0;
12         }
13         if(n == 1)
14         {
15             return 1;
16         }
17         int sum = 0;
18         int j = 0;
19         for(int i =0; i < n; i++)
20         {
21             if(nums[i] != val)
22             {
23                 nums[j] = nums[i];
24                 j++;
25                 sum++;
26             }
27         }
28         return sum;
29     }
30 };

哈哈, 又一次100%。

时间: 2024-08-28 09:31:16

leetcode 27的相关文章

图解双指针 | LeetCode 27. 移除元素

题目描述 原题链接:LeetCode 27. 移除元素 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 元素的顺序可以改变.你不需要考虑数组中超出新长度后面的元素. 示例 1: 给定 nums = [3,2,2,3], val = 3, 函数应该返回新的长度 2, 并且 nums 的前两个元素均为 2. 你不需要考虑数组中超出新长度后面的元素

LeetCode(27)题解:Remove Element

https://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 be changed. It doesn't matter what you leave beyond the new length. 思路: 因为可以改变元

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.

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 (删除元素) 解题思路和方法

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 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[

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

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题一样,就是判断条件不一样而已. 题目大意: 给一个数组,要求返回删除