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

Solution 1: 设置一个新的下标length,遍历过程中遇到val就跳过,否则就赋给新数组下标对应元素。缺点是有多余的赋值。

 1 class Solution {
 2 public:
 3     int removeElement(vector<int>& nums, int val) {
 4         if(nums.empty())return 0;
 5         int length=0;
 6         for(int i=0;i<nums.size();i++){
 7             if(nums[i]!=val)
 8                 nums[length++]=nums[i];
 9             else
10                 continue;
11         }
12         return length;
13     }
14 };

Solution 2:最优美的解法,当遍历过程中遇到val,就用末尾的元素来填补。这样甚至遍历不到一次。注意:从末尾换过来的可能仍然是elem,因此i--,需要再次判断。

 1 class Solution {
 2 public:
 3     int removeElement(vector<int>& nums, int val) {
 4         int newlength = nums.size();
 5         for(int i = 0; i < newlength; i ++)
 6         {
 7             if(nums[i] == val){
 8                 nums[i] = nums[newlength-1];
 9                 i--;
10                 newlength--;
11             }
12         }
13         return newlength;
14     }
15 };
时间: 2024-10-05 23:22:51

【LeetCode】27 - Remove Element的相关文章

【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 matt

【LeetCode】027. 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 mat

【easy】27. Remove Element

删除等于n的数,并返回剩余元素个数 Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the first two elements of nums being 2. class Solution { public: int removeElement(vector<int>& nums, int val) { int count = 0; int len = nums.size()

LeetCode OJ 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][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

【leetcode】Find Peak Element

Find Peak Element A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one

【Leetcode】Kth Smallest Element in a Sorted Matrix

题目链接:https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ 题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smallest eleme

【leetcode】26. Remove Duplicates from Sorted Array

题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. 解题分析: 扫描一遍链表,用一个变量标记已找到的不重复的元

【LeetCode】26. Remove Duplicates from Sorted Array 解题小结

题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array n