27. Remove Element

Easy

Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example 1:

  1. Given nums = [3,2,2,3], val = 3,
  2.  
  3. Your function should return length = 2, with the first two elements of nums being 2.
  4.  
  5. It doesn't matter what you leave beyond the returned length.

Example 2:

  1. Given nums = [0,1,2,2,3,0,4,2], val = 2,
  2.  
  3. Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
  4.  
  5. Note that the order of those five elements can be arbitrary.
  6.  
  7. It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

  1. // nums is passed in by reference. (i.e., without making a copy)
  2. int len = removeElement(nums, val);
  3.  
  4. // any modification to nums in your function would be known by the caller.
  5. // using the length returned by your function, it prints the first len elements.
  6. for (int i = 0; i < len; i++) {
  7.     print(nums[i]);
  8. }
  1. package leetcode.easy;
  2.  
  3. public class RemoveElement {
  4. @org.junit.Test
  5. public void test() {
  6. int[] nums11 = { 3, 2, 2, 3 };
  7. int[] nums12 = { 0, 1, 2, 2, 3, 0, 4, 2 };
  8. int[] nums21 = { 3, 2, 2, 3 };
  9. int[] nums22 = { 0, 1, 2, 2, 3, 0, 4, 2 };
  10. int val1 = 3;
  11. int val2 = 2;
  12. System.out.println(removeElement1(nums11, val1));
  13. System.out.println(removeElement1(nums12, val2));
  14. System.out.println(removeElement2(nums21, val1));
  15. System.out.println(removeElement2(nums22, val2));
  16. }
  17.  
  18. public int removeElement1(int[] nums, int val) {
  19. int i = 0;
  20. for (int j = 0; j < nums.length; j++) {
  21. if (nums[j] != val) {
  22. nums[i] = nums[j];
  23. i++;
  24. }
  25. }
  26. return i;
  27. }
  28.  
  29. public int removeElement2(int[] nums, int val) {
  30. int i = 0;
  31. int n = nums.length;
  32. while (i < n) {
  33. if (nums[i] == val) {
  34. nums[i] = nums[n - 1];
  35. // reduce array size by one
  36. n--;
  37. } else {
  38. i++;
  39. }
  40. }
  41. return n;
  42. }
  43. }

LeetCode_27. Remove Element的更多相关文章

  1. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  2. [LeetCode] Remove Element 分析

    Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...

  3. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  4. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. [array] leetCode-27. Remove Element - Easy

    27. Remove Element - Easy descrition Given an array and a value, remove all instances of that value ...

  7. leetcode-algorithms-27 Remove Element

    leetcode-algorithms-27 Remove Element Given an array nums and a value val, remove all instances of t ...

  8. 【LeetCode】27. Remove Element (2 solutions)

    Remove Element Given an array and a value, remove all instances of that value in place and return th ...

  9. [LeetCode] Remove Element题解

    Remove Element: Given an array and a value, remove all instances of that value in place and return t ...

随机推荐

  1. 【Java】Eclipse+环境安装及新建Project

    安装Eclipse 1.进入ecilpse官方下载地址:https://www.eclipse.org/downloads/?FEATURED_STORY 2.点击红色箭头指向的Download Pa ...

  2. JSOI2009 密码 和 JSOI2007 文本生成器 和 ZOJ3545 Rescue the Rabbit

    密码 众所周知,密码在信息领域起到了不可估量的作用.对于普通的登陆口令,唯一的破解 方法就是暴力破解一逐个尝试所有可能的字母组合,但这是一项很耗时又容易被发现的工 作.所以,为了获取对方的登陆口令,在 ...

  3. SVN工作区同步

    单击“团队同步”菜单项或“团队同步”视角的“团队”工具栏上的“同步”按钮后,“同步视图”中将显示SVN工作区同步.它提供了从远程检查本地副本的更改类型的概率. 这是“同步视图”中的 “SVN工作空间同 ...

  4. fs模块

    fs.readdir(path, callback) 异步读取目录下文件 path - 文件路径. callback - 回调函数,回调函数带有两个参数err, files,err 为错误信息,fil ...

  5. Number of Islands II

    Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Origina ...

  6. selenium之python源码解读-expected_conditions

    一.expected_conditions 之前在 selenium之python源码解读-WebDriverWait 中说到,until方法中method参数,需要传入一个function对象,如果 ...

  7. Spring事务管理器

    1.创建实体和接口 public class Bank { private Integer id; private String name; private String manay; public ...

  8. PHP+下载文件夹

    php下载文件我整理了这三种方法,和大家分享一下: 第一种:直接添加文件下载的绝对路径连接 //如:我有一个文件在demo.xx.cn/demo.zip<button>    <a ...

  9. centos7mongo集群

    1.安装 cat > /etc/yum.repos.d/mongodb.repo << EOF[mongodb-org-3.6]name=MongoDB Repositorybase ...

  10. Hadoop界的Hello World!

    Hadoop界的Hello World! 2019-05-20  19:50:09 应用平台:Eclipse+ubantu+hadoop包 注:例分析的形式给宝宝们解释一下,详细运行过程省略. 实例: ...