C# 写 LeetCode easy #27 Remove Element
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 ofnumscontaining0,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的更多相关文章
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 【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 ...
- LeetCode:27. Remove Element(Easy)
1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...
- 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 ...
- 【LeetCode】27. Remove Element 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...
- 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 ...
- 【LeetCode】27 - Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 【一天一道LeetCode】#27. Remove Element
一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...
- 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 ...
随机推荐
- mac iterm2 安装 lrzsz rz sz命令
原文:https://blog.csdn.net/jack85986370/article/details/51382077 首先mac自带的终端是不支持lrzsz的,需要下载安装iterm2,下载地 ...
- 算法(Algorithms)第4版 练习 1.5.10
Yes, but it could increase the tree height, so the performance guarantee would be invalid.
- CheckStyle:unable to parse configuration stream - Element type "message" must be declared
版本在1.3以上,包括1.3: <!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1. ...
- spring与mybati整合方法
(1)spring配置文件: <!-- 引入jdbc配置文件 --> <context:property-placeholder location="jdbc.proper ...
- typedarrays splice
TypedArrays 不是一个典型的 数组类型,所以不存在 splice 方法.但是可以模拟实现 function splice(arr, starting, deleteCount, elemen ...
- leetcode 201. Bitwise AND of Numbers Range(位运算,dp)
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- 3.3 CCSprite 精灵详解
3.3.1 创建精灵常用的 4 种方式 (当然还有其他方式,只不过 这四种比较常用) //创建精灵常用的 4 种方式 CCSprite* spr1 = CCSprite::create(const c ...
- URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
解决:鼠标悬于上方Alt + Enter 选择Ignore
- Gson小记
Gson过滤字段,只要在字段前面添加“transient”关键字即可:之前就是因为Channel字段序列化的时候导致了stack over异常.
- 洛谷 4178 Tree——点分治
题目:https://www.luogu.org/problemnew/show/P4178 点分治.如果把每次的 dis 和 K-dis 都离散化,用树状数组找,是O(n*logn*logn),会T ...