1、题目

Given an array and a value, 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 in place with constant memory.

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

Example:
Given input array nums = [3,2,2,3], val = 3

Your function should return length = 2, with the first two elements of nums being 2.

2、代码实现

java:
public class Solution {public int removeElement(int[] nums, int val) {if (nums == null || nums.length == 0)return 0;int count = 0;for (int i = 0; i < nums.length; ++i) {if (nums[i] != val) {nums[count++] = nums[i];}}return count;}
}
C++:
class Solution {
public:int removeElement(vector<int>& nums, int val) {int count = 0;for (int i = 0; i < nums.size(); ++i) {if (nums[i] != val) {nums[count++] = nums[i];}}return count;}
};

3、总结

其实非常简单,把不相等的数据从头到尾放进数组就行

LeetCode之Remove Element相关推荐

  1. leetcode 27. Remove Element

    问题描述: Given an array and a value, remove all instances of that value in-place and return the new len ...

  2. LeetCode算法入门- Remove Element -day20

    LeetCode算法入门- Remove Element -day20 1. 题目描述 Given an array nums and a value val, remove all instance ...

  3. [LeetCode] Remove Element 分析

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

  4. [leetcode]83.Remove Duplicates from Sorted List

    题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...

  5. [勇者闯LeetCode] 83. Remove Duplicates from Sorted List

    [勇者闯LeetCode] 83. Remove Duplicates from Sorted List Description Given a sorted linked list, delete ...

  6. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] c++

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  7. LeetCode——Kth Largest Element in an Array

    LeetCode--Kth Largest Element in an Array Question Find the kth largest element in an unsorted array ...

  8. [LeetCode] 169. Majority Element 多数元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. LeetCode:Remove Nth Node From End of List 移除链表倒第n项

    2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 http ...

最新文章

  1. C++实现判断两个单词是否为变位词
  2. CTF-IDA的常用操作(初学者)
  3. c语言ascii码表数字,求教!我想显示数字但是现在显示的却是数字在ASCII码中对应的符...
  4. [深度学习] FM FFM 算法基本原理
  5. 知识图谱需要解决的问题
  6. mysql and 和where,关于mysql:连接sql查询中where和and子句的区别
  7. PATB1017 A除以B
  8. vim插件管理利器:pathogen
  9. 猿如意中的【取色器】效率工具详情介绍
  10. 债务人无力偿还,债权人可否直接起诉“次债务人”
  11. 【02】一个实现h5的拖放的整个过程-魔芋
  12. VMX(1) -- 简介
  13. app软件小程序开发
  14. 单精度浮点型(float)和双精度浮点型(double)的区别
  15. 积微——荀子《强国篇》,给每个职场人士推荐
  16. Java学习笔记之基础语法(一)
  17. word2vec and glove
  18. PDF格式的文档如何编辑修改
  19. 2021-10-17idea无法导入依赖
  20. PPT的三种抠图方法-演示

热门文章

  1. OAuth 2.1 的进化之路
  2. 为什么async/await方法不能有lock或Monitor
  3. WPF 命中测试HitTest
  4. 使用 Tye 辅助开发 k8s 应用竟如此简单(二)
  5. 引入Jaeger——使用
  6. .NET 5干货来袭 嘉宾李杨桂素伟
  7. 聊一聊ABP vNext的模块化系统
  8. 如何在 C# 中使用 RabbitMQ
  9. 设计一个具有等待队列的连接池
  10. 不懂数据库索引原理?因为你心里没有一点B树