Remove Element 解答

Question

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 -- Two Pointers

Remember that p1 starts from -1.

 1 public class Solution {
 2     public int removeElement(int[] nums, int val) {
 3         int length = nums.length;
 4         if (length == 0)
 5             return 0;
 6         if (length == 1) {
 7             if (nums[0] == val)
 8                 return 0;
 9             else
10                 return 1;
11         }
12         int p1 = -1, p2 = 0;
13         while (p2 < length) {
14             if (nums[p2] != val) {
15                 p1++;
16                 nums[p1] = nums[p2];
17                 p2++;
18             } else {
19                 p2++;
20             }
21         }
22         return p1 + 1;
23     }
24 }
时间: 2024-10-19 08:13:25

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] Remove Element 分析

Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接: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. I

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:Remove Duplicates from Sorted Array &amp;&amp; Remove Element

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 A =

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.

LeetCode26/27/80 Remove Duplicates from Sorted Array I and II/Remove Element**

一: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 m

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. 中文:给定一个数组和一个数值,去除这个数值所有出现位置,并返回新数组的长度. 元素的顺序可以改变.除了新的长度,你

Leetcode 线性表 Remove Element

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Element Total Accepted: 13840 Total Submissions: 42676 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 change

【LeetCode】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. 解法一:使用vector记录每个elem的位置,介于这些位置之间的元素就可以前移相应