27. Remove Element【leetcode】

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.

题意:从数组中移除所有的给定的某个值,并返回数组新的长度;最后一句的意思是在新的长度后面的元素是任意的,没有顺序的要求,只要是之前数组中的元素即可,后面的元素无所谓。

 public class Solution {
public int removeElement(int[] nums, int val) {
int len=nums.length;
int count = 0;
for(int i=0;i<len;i++){
if(nums[i]==val){
for(int j=i;j<len-1;j++){
nums[j]=nums[j+1];
}
count++;
}
}
return len-count;
}
}

解题思路:

  1. 进行循环遍历,如果只输出新数组的长度,那么只需要记录下有多少个值和标记值相同即可,最终输出数组长度-计数值
  2. 如果需要返回新的数组删除原有数组,理论上来说新建一个数组,然后将标记的不复制到新数据即可,但是题中表明,只允许在内存中进行操作,不让新建数组,那么此时如果第i个位置有标记值,那么将i+1....到最后一个值都向左挪一格即可。
  3. 实现方式请见代码

27. Remove Element【leetcode】的更多相关文章

  1. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  2. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  3. 【LeetCode】556. Next Greater Element III 解题报告(Python)

    [LeetCode]556. Next Greater Element III 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

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

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

  5. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  6. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  7. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  8. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

  9. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

随机推荐

  1. Itunes制作手机铃声,图文版

    一.下载歌曲,选择歌曲用itunes打开,打开出现下面界面 二.设置歌曲 右键点击歌曲,找到显示简介,点击选项,截取音乐,出现下图: 截取你喜欢的部分,点击确定 点击确定后,选中该歌曲,找到左上方 文 ...

  2. zookeeer client 通信协议

    这里主要记录zookeeper client通信协议的.在官方的文档里没找到协议相关部分.这里是记录的协议是通过分析客户端代码得来的. 一.通信流程 客户端发起连接,发送握手包进行timeout协商, ...

  3. android studio创建一个最简单的跳转activity

    实现目的:由mainActivity跳转到otherActivity 1.写好两个layout文件,activity_main.xml和otherxml.xml activity_main.xml & ...

  4. 使用pycurl探测web服务质量

    1:pycurl模块的安装方法 easy_install pycurl pip install pycurl 2:示例代码如下,是在python3下实现的,如若使用python2稍作修改即可 # -* ...

  5. 关于Lumen / Laravel .env 文件中的环境变量是如何生效的

    .env 文件包含默认环境变量,我们还可自定义其他任何有效的变量,并可通过  调用 env() 或 $_SERVER 或 $_ENV  来获取该变量.那么env()是如何加载到这些变量的呢?在Lume ...

  6. gulp静态资源构建、压缩、版本号添加

    公司移动端商城使用前后分离方案,前台nginx静态文件,js使用requirejs模式,使用gulp压缩添加版本号时发现问题, 问题1.在公共的js配置中,引用的路径是写死的,缓存会一直存在. 解决方 ...

  7. github 项目管理

    一.在GitHub上创建项目 1. 打开浏览器 在地址栏输入地址:github.com 填写用户名.邮箱.密码 点击Sign up即可简单地注册 2. 完成注册,进入github平台, 点击new r ...

  8. 数位dp模板 [dp][数位dp]

    现在才想到要学数位dp,我是不是很弱 答案是肯定的 以一道自己瞎掰的题为模板 //题: //输入数字n //从0枚举到n,计算这n+1个数中含有两位数a的数的个数 //如12930含有两位数93 #i ...

  9. oracle-sql入门练习及答案

    1,通过命令行方式打开sqlplus2,执行登录命令sqlplus scott/scott@192.168.248.129/orcl3进行sqlplus命令测试Set time onSet pages ...

  10. Windows 10 IoT Serials 9 – 如何利用IoTCoreAudioControlTool改变设备的音频设备

    大家知道,在Windows 10 IoT Core上,如果用户外接了USB声卡.带有麦克风的摄像头之类的硬件,就会有多个音频设备可以用.但是,系统目前并没有提供直接的UI来设置音频的输入或者输出设备. ...