27. Remove Element - 移除元素-Easy
Description:
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.
思路分析:
1.最终要求的是输出除给定值以外的其他数组中的值,所写方法需要返回的是这些值的总个数,即输入[3,2,2,3]==>输出:2,并且数组的新状态为[2,2,3,3];
2.最近在复习排序算法,所以这个问题很亲切,其中也一种排序的影子,只不过是把特定的值而不是最大值放到最后,问题还有一个约束:只能用常数级的额外空间,即O(n)=1,所以不能通过额外的数组来记录给定值以外的值完成;
3.因此,排序里面每一次循环里怎么把局部最大值移动到局部的最后,这里也可沿用,只不过,这里问题有其特殊性:值一旦已经确定了,不需要后续的一轮比较来确定(排序的时候最大值需要这样来确定),一旦遍历到就可以直接当做‘最大值’来移动到最左边,而且最大值只有一个,但特定值可以同时出现。
C#代码:
public class Solution {
public int RemoveElement(int[] nums, int val) {
int j=nums.Length-,len= nums.Length,i=;
while(j>=i){
if(nums[j]==val){
len--;
j--;
continue;
}
if(nums[i]==val){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
len--;
j--;
}else{
i++;
}
if(i==j){
break;
}
}
return len;
}
}
或者:
public class Solution {
public int RemoveElement(int[] nums, int val) {
int len= nums.Length;
for(int i=;i<nums.Length;i++){
if(i==len)break;
if(nums[i]==val){
if(nums[len-]==val){
len--;
i--;
continue;
}
int temp = nums[i];
nums[i] = nums[len-];
nums[len-] = temp;
len--;
}
}
return len;
}
}
27. Remove Element - 移除元素-Easy的更多相关文章
- [LeetCode] 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移除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- [LeetCode] Remove Element 移除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 027 Remove Element 移除元素
给定一个数组和一个值,在这个数组中原地移除指定值和返回移除后新的数组长度.不要为其他数组分配额外空间,你必须使用 O(1) 的额外内存原地修改这个输入数组.元素的顺序可以改变.超过返回的新的数组长度以 ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- leetCode练题——27. Remove Element
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
- 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 ...
随机推荐
- shiro.ini 配置详解
引用: [1]开涛的<跟我学shiro> [2]<SpringMVC整合Shiro> [3]<shiro简单配置> [4]Apache shiro集群实现 (一) ...
- WKWebView的使用与JS交互详细解读
前言: WKWebView 这是在iOS8.0之后增加的一个比UIWebView更加完善和强大的控件!看网上关于它的博客也是有许多的了,从各个方面总结一下这个WKWebView看网上说它主要是为了和J ...
- 网络爬虫与搜索引擎优化(SEO)
爬虫及爬行方式 爬虫有很多名字,比如web机器人.spider等,它是一种可以在无需人类干预的情况下自动进行一系列web事务处理的软件程序.web爬虫是一种机器人,它们会递归地对各种信息性的web站点 ...
- jquery实现显示和隐藏toggle()方法的使用
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- 通过映射关系 动态转义为统一格式的数据 (支持 JSON 和 XML )
在很多的时候 我们都会 需要 将不同格式的数据 转换为 统一的数据格式 比如 将Json 源数据 { "b": [ { "c": "referenc ...
- BZOJ 4085:[Sdoi2015]bigyration(SDOI 2015 round 2 Day 1)
别人家的神选系列.Day2根本不能做QAQ 题目描述:给定两个字符串集合,一个长度为n,另一个为m,求有多少个数字对i,j,满足xi+yj能由一个(n+m)/2的字符串旋转拼接而成 我们枚举长度较长的 ...
- Spark 键值对RDD操作
键值对的RDD操作与基本RDD操作一样,只是操作的元素由基本类型改为二元组. 概述 键值对RDD是Spark操作中最常用的RDD,它是很多程序的构成要素,因为他们提供了并行操作各个键或跨界点重新进行数 ...
- SaberRD之交流分析
交流分析(AC Analysis)也叫做小信号(Small-Signal)分析,也即分析电路的小信号频率响应,更严谨的定义是:分析工作在直流偏置电压下的非线性电路对于一定频率范围内的输入小信号的系统响 ...
- 读书笔记 effective c++ Item 23 宁可使用非成员非友元函数函数也不使用成员函数
1. 非成员非友元好还是成员函数好? 想象一个表示web浏览器的类.这样一个类提供了清除下载缓存,清除URL访问历史,从系统中移除所有cookies等接口: class WebBrowser { pu ...
- 云服务器spark集群搭建
---恢复内容开始--- 1:去官网下载spark http://spark.apache.org 2:解压,然后在自己的机器上编译conf中的两个文件 mv slaves.template slav ...