Leetcode: Remove Elements

一次性通过的,比较顺利,从读题到编写到检查到通过,14分50秒,我在不断进步中,相信经过一段时间联系,这种题可以一眼就写出来,不超过5分钟。

这道题应该说方法跟 Remove Duplicates from sorted Array挺类似的

My Solution:


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

再贴个另外两个人的solution,以便对照参考,比较优劣:

Solution 1: 这个想法有点曲折


 1 public class Solution {
2 public int removeElement(int[] A, int elem) {
3 // Start typing your Java solution below
4 // DO NOT write main() function
5 int i=0, j=A.length-1;
6
7 while(i<=j){
8 if(A[i]==elem)
9 swap(A,i,j--);
10 else
11 i++;
12 }
13 return j+1;
14 }
15
16 public void swap(int[] A,int i, int j){
17 int temp = A[i];
18 A[i] = A[j];
19 A[j] = temp;
20 }
21 }

Solution 2: 跟我的想法一致


 1 public class RemoveElement {
2 public int removeElement(int[] A, int elem) {
3 // Start typing your Java solution below
4 // DO NOT write main() function
5 if (A.length == 0) {
6 return 0;
7 }
8 int counter = 0;
9 for (int i = 0; i < A.length; i++) {
10 if (A[i] != elem) {
11 A[counter] = A[i];
12 counter++;
13 }
14 }
15 return counter;
16
17 }
18 }

Leetcode: Remove Elements,布布扣,bubuko.com

时间: 2024-10-12 22:31:18

Leetcode: Remove Elements的相关文章

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

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

[题目] 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

[LeetCode] Remove Element [20]

题目 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 Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I 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 memor

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

[leetcode]Remove Duplicates from Sorted List II @ Python

原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4-&g

LeetCode: Remove Nth Node From End of List [019]

[题目] Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: G

[LeetCode] Remove Duplicates from Sorted Array II [27]

题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 原题链接(点我) 解题思路 移除数组中重复次数超过2次以上出现的数,但是可以允许重复2次