27、 Remove Element

  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:

Given nums = [3,2,2,3], val = 3,

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

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,1,2,2,3,0,4,2], val = 2,

Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.

Note that the order of those five elements can be arbitrary.

It doesn't matter what values are set beyond the returned length.

代码:
static void Main(string[] args)
{
int[] nums = { , , , , , , };
Console.WriteLine(RemoveElement(nums,));
Console.ReadKey();
} private static int RemoveElement(int[] nums, int val)
{
int index = ;
for(int i=;i<nums.Length;i++)
{
if (nums[i] != val)
{
nums[index] = nums[i];
index++;
}
}
return index;
}

解析:

输入:数组和某个元素

输出:删除后的数组元素个数

代码思想

  用index记录在值不相同时的索引,循环遍历元素,若不相同将记录索引处的值用当前值覆盖,最后增加索引计数。

时间复杂度:O(n)

C# 写 LeetCode easy #27 Remove Element的更多相关文章

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

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

  2. 【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 ...

  3. LeetCode:27. Remove Element(Easy)

    1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...

  4. LeetCode Array Easy 27. Remove Element 解题

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  5. 【LeetCode】27. Remove Element 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...

  6. Leetcode No.27 Remove Element(c++实现)

    1. 题目 1.1 英文题目 Given an integer array nums and an integer val, remove all occurrences of val in nums ...

  7. 【LeetCode】27 - Remove Element

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

  8. 【一天一道LeetCode】#27. Remove Element

    一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...

  9. C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array

    26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...

随机推荐

  1. 算法(Algorithms)第4版 练习 1.3.4

    主要思路: 遇到左括号则一直压栈,遇到右括号时则从栈中弹出一个元素. 如果此时栈为空,则返回false. 如果这个元素与右括号不匹配,则返回false. 重复此过程,最后判断栈是否为空,若为空则返回t ...

  2. Tomcat翻译--Tomcat Web Application Deployment(Tomcat中部署web应用)

    原文:http://tomcat.apache.org/tomcat-7.0-doc/deployer-howto.html Introduction(介绍) Deployment is the te ...

  3. Building Performant Expand & Collapse Animations

    t's starting to be pretty common knowledge that there are only 2 things you can animate cheaply in C ...

  4. Tomcat_异常_02_IOException while loading persisted sessions: java.io.EOFException

    异常原因: EOFException表示输入过程中意外地到达文件尾或流尾的信号,导致从session中获取数据失败. 这是由于tomcat上次非正常关闭时有一些活动session被持久化(表现为一些临 ...

  5. Oracle学习笔记_01_SQL初步

    1.分类 SQL语句分为以下三种类型: DML: Data Manipulation Language        数据操纵语言       DDL: Data Definition Languag ...

  6. 一个类的类类型是Class类的实例,即类的字节码

    new 是静态加载类,编译时期加载.一遍功能性的类 需要动态加载

  7. 常规DLL与扩展DLL区别

    1.常规DLL可以被各种程序(python,VB等)调用,扩展DLL只能被MFC程序调用.提供给外部使用的控件类只能用扩展DLL. 2.扩展DLL的入口函数是DllMain(),而常规DLL入口是继承 ...

  8. linux命令学习笔记(58):telnet命令

    telnet命令通常用来远程登录.telnet程序是基于TELNET协议的远程登录客户端程序.Telnet协议是TCP/IP协议族 中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用 ...

  9. 数据交换格式XML和JSON对比

    1.简介: XML:extensible markup language,一种类似于HTML的语言,他没有预先定义的标签,使用DTD(document type definition)文档类型定义来组 ...

  10. A唐纳德先生和假骰子(华师网络赛)

    Time limit per test: 1.0 seconds Memory limit: 256 megabytes 在进行某些桌游,例如 UNO 或者麻将的时候,常常会需要随机决定从谁开始.骰子 ...