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 ofnums
containing0
,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 ...
随机推荐
- Luogu-2600 [ZJOI2008]瞭望塔
把地面看成半平面,能看到所有位置的点所在的区域即为半平面的交 因为分段函数的极值只会在转折处或边界取到,所以对于半平面上和地面上的每一个交点都求一下距离就好了 #include<cmath> ...
- 算法(Algorithms)第4版 练习 2.2.9
package com.qiusongde; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdOut; publi ...
- Shiro-自定义realm
Shiro自定义 realm package com.zhen.realm; import org.apache.shiro.authc.AuthenticationException; import ...
- virtualbox 相关操作
关闭 vboxmanage controlvm nenew poweroff 打开,后台运行 vboxmanage startvm centos0 --type headless 复制虚拟机镜像VBo ...
- Hibernate - 设置隔离级别
JDBC 数据库连接使用数据库系统默认的隔离级别. 在 Hibernate 的配置文件中可以显式的设置隔离级别. 每一个隔离级别都对应一个整数: 1. READ UNCOMMITED2. READ C ...
- IE input 去掉文本框的叉叉和密码输入框的眼睛图标
::-ms-clear, ::-ms-reveal{display: none;}
- Agc012_E Camel and Oases
传送门 题目大意 坐标轴上有$n$个坐标,第$i$个坐标是$x_i$,初始你有一个容量$V$,当两个给定的坐标距离不超过$V$时,你可以从一个坐标到达另一个坐标,同时你还可以令$V=\lfloor \ ...
- Oracle 12c 新特性之 temp undo
Oracle 12c R1 之前,临时表生成的undo记录是存储在undo表空间里的,通用表和持久表的undo记录也是类似的.而在 12c R12 的临时 undo 功能中,临时 undo 记录可以存 ...
- 无密码登录Linux服务器
1.使用windows上的SecureCRT生成密钥对. Tools->Create Public Key..->RSA->Passphrase(最好输入,也可为空)->长度默 ...
- fastjson 使用笔记
1.string转json String params={'key1':'50001','key2':10007700'}Map<String, String> a = JSON.pars ...