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.

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

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

My Solution:

 public class Solution {
public int removeElement(int[] A, int elem) {
int count = 0;
for(int i=0; i<A.length; i++){
if(A[i] != elem){
A[count] = A[i];
count++;
}
}
return count;
}
}

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

Solution 1: 这个想法有点曲折

 public class Solution {
public int removeElement(int[] A, int elem) {
// Start typing your Java solution below
// DO NOT write main() function
int i=0, j=A.length-1; while(i<=j){
if(A[i]==elem)
swap(A,i,j--);
else
i++;
}
return j+1;
} public void swap(int[] A,int i, int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}

Solution 2: 跟我的想法一致

 public class RemoveElement {
public int removeElement(int[] A, int elem) {
// Start typing your Java solution below
// DO NOT write main() function
if (A.length == 0) {
return 0;
}
int counter = 0;
for (int i = 0; i < A.length; i++) {
if (A[i] != elem) {
A[counter] = A[i];
counter++;
}
}
return counter; }
}

Leetcode: Remove Elements的更多相关文章

  1. LeetCode:Remove Duplicates from Sorted List I II

    LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...

  2. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  3. Javacript Remove Elements from Array

    參考自: https://love2dev.com/blog/javascript-remove-from-array/ 1. Removing Elements from End of Array ...

  4. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  5. [LeetCode] Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  6. LeetCode Remove Linked List Elements 删除链表元素

    题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...

  7. leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;

    ,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...

  8. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  9. [LeetCode] Remove Element 移除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

随机推荐

  1. a computer-centered view of information systems to a database-centered view

    Code.Complete.Second.Edition 2004 Bachman compared the Ptolemaic-to-Copernican change in astronomy t ...

  2. synchronized的使用方法

    [转自] http://blog.csdn.net/witsmakemen/article/details/6966116 记下来,很重要. Java语言的关键字,当它用来修饰一个方法或者一个代码块的 ...

  3. nrf51822裸机教程-硬件timer

    该讲介绍51822的Timer/Counter模块工作在timer模式下(定时器模式,还可以工作为计数器模式) 如何操作 51822的Timer/Counter结构如下图所示 Timer模块从PCLK ...

  4. nrf51822裸机教程-GPIOTE

    GPIO通常都会具有中断功能,上一讲的GPIO中并没有涉及到中断的相关寄存器. 51822将GPIO的中断相关做成了一个单独的模块GPIOTE,这个模块不仅提供了GPIO的中断功能,同时提供了 通过t ...

  5. docker debug diagnose

    $ sudo systemctl stop docker $ sudo docker -d -D DEBU[0282] Error contacting registry https://regist ...

  6. 设计模式:享元模式(Flyweight)

    定   义:运用共享技术有效地支持大量细粒度的对象. 结构图: 内部状态:在享元对象内部并且不会随环境而改变的共享部分. 外部状态:随环境改变而改变的.不可共享的状态. Flyweight类,具体享元 ...

  7. 使用C#发送正文带图片邮件

    最近有个地方用到正文带图片的邮件发送功能,由于发送邮件调用的是web service,要求正文必须是string,而接收方要能看到图片,还不能单纯的添加一个图片地址链接,查阅了很多资料,基本上都是从头 ...

  8. Progress Reporting

    Progress reporting is a key activity of project management. The project manager issues regular repor ...

  9. Java学习-018-EXCEL 文件写入实例源代码

    众所周知,EXCEL 也是软件测试开发过程中,常用的数据文件导入导出时的类型文件之一,此文主要讲述如何通过 EXCEL 文件中 Sheet 的索引(index)或者 Sheet 名称获取文件中对应 S ...

  10. 我的第一个WCF程序,很简单适合我等菜鸟

    1.首先我罗列一下网站搜索并经过自己理解的WCF的含义: 1)WCF:(WIndows Communication Foundation)是由微软是由微软发展的一组数据通信的应用开发接口,可以翻译为W ...