Remove Element leetcode java

题目:

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.

题解:

这道题跟remove duplicate sorted array是一样的。。。还是双指针,一个帮忙记录length外加替换不是elem的值,一个帮忙往前找。

代码如下:

1     public int removeElement(int[] A, int elem) {
 2         if(A==null||A.length == 0)
 3             return 0;
 4             
 5         int len = 0;
 6         for(int i =0; i < A.length;i++){
 7             if(A[i] != elem){
 8                 if(A[len]!=A[i])
 9                     A[len]=A[i];
10                 len++;
11             }
12         }
13         return len;
14     }

时间: 2024-11-09 02:43:57

Remove Element leetcode java的相关文章

leetcode Remove Element(easy) /java

和上一道题思路差不多. import java.io.*; import java.util.*; public class Solution { public static int removeElement(int[] nums, int val) { int r=0; int len=nums.length; int i=0,j=0; if(len==0) return 0; int c=0; for(i=0;i<len;i++) { if(val!=nums[i]) { nums[j]=

Remove Element -- leetcode

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上执行时间为36ms. class Solution { publ

169 Majority Element [LeetCode Java实现]

题目链接:majority-element /** * Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array.

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

Remove Duplicates from Sorted List leetcode java

题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 题解: 这道题是经典的双指针问题,用两个指针一前一后指向链表.如果两个指针指向的值相等,那么就让第二个指针一直

[LeetCode] 027. Remove Element (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 027. Remove Element (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-element/ 代码(github):https://github.com/illuz/leetcode 题意: 删除一个数组里值为 elem 的所有数. 分析: 用

[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: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 线性表 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