27. Remove Element【leetcode】

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.

题意:从数组中移除所有的给定的某个值,并返回数组新的长度;最后一句的意思是在新的长度后面的元素是任意的,没有顺序的要求,只要是之前数组中的元素即可,后面的元素无所谓。

 1 public class Solution {
 2     public int removeElement(int[] nums, int val) {
 3         int len=nums.length;
 4         int count = 0;
 5         for(int i=0;i<len;i++){
 6             if(nums[i]==val){
 7                 for(int j=i;j<len-1;j++){
 8                     nums[j]=nums[j+1];
 9                 }
10                 count++;
11             }
12         }
13         return len-count;
14     }
15 }

解题思路:

  1. 进行循环遍历,如果只输出新数组的长度,那么只需要记录下有多少个值和标记值相同即可,最终输出数组长度-计数值
  2. 如果需要返回新的数组删除原有数组,理论上来说新建一个数组,然后将标记的不复制到新数据即可,但是题中表明,只允许在内存中进行操作,不让新建数组,那么此时如果第i个位置有标记值,那么将i+1....到最后一个值都向左挪一格即可。
  3. 实现方式请见代码
时间: 2024-09-30 08:06:19

27. Remove Element【leetcode】的相关文章

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】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio

【LeetCode】双指针 two_pointers(共47题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [19]Remove Nth Node From End of List [26]Remove Duplicates from Sorted

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

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][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】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

【LeetCode】Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example:A =