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. tableView在加载数据成功之前先展示了footerView-医生工作台1期

    一进入这个页面先显示了footreView   解: 发现先走的requestData,但是请求接口成功的回调没走,走了configUI   configUI之后,走了requestData   所以 ...

  2. Java中测试对象的等价性

    Java中用于测试对象的等价性有三个操作符:== , != 和 Equals() 对于基本类型即int,boolean, byte 等等来说,==和 != 比较的是 基本类型的内容,这和c.c++是一 ...

  3. TabHost详解

    [转]http://blog.csdn.net/harvic880925/article/details/17120325 前言:今天仔细研究了下TabHost,主要是为了实现微信底部导航栏的功能,最 ...

  4. java BufferedWriter and BufferedReader

    //Listing 5-2. Demonstrating the BufferedWriter and BufferedReader Classes import java.io.BufferedRe ...

  5. python socket 选项

    一.int socket(int domain, int type, int protocol) 1.domain -- 指定使用何种的地址类型 PF_INET, AF_INET: Ipv4网络协议P ...

  6. Qt from Linux to Windows target

    45down voteaccepted Just use M cross environment (MXE). It takes the pain out of the whole process: ...

  7. 过滤android应用列表(区分系统应用、第三方应用、sd卡中的应用)

    if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { // 系统程序 }else if ((app.flags & Applica ...

  8. ArcGIS三大文件格式解析

    原文:ArcGIS三大文件格式解析 Shape数据 Shapefile是ArcView GIS 3.x的原生数据格式,属于简单要素类,用点.线.多边形存储要素的形状,却不能存储拓扑关系,具有简单.快速 ...

  9. ByteBuffer

    1.堆内:HeapByteBuffer,在java的堆内创建. 缺点:可能引起堆的不断gc 写文件的时候需要先将堆的buffer写进直接buffer里,然后再写入文件 2.堆外:DirectByteB ...

  10. 通过SessionID和用户名来保证同一个用户不能同时登录(单点登录)

    可以通过SessionID和用户名来保证同一个用户不能同时登录的问题,下面程序模仿了QQ的登录,当登录后判断当前帐号是否已经登录,如果登录.则踢掉以前登录的用户. 1.通过Application全局变 ...