LeetCode第27题

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.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val); // any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}

翻译:

给定一个数组和一个定值,删除数组中的所有此值并返回一个新的数组长度

不要申明额外的数组空间,必须保证算法复杂度为O(1)

数组的顺序无所谓

思路:

这一题和【LeetCode算法-26】Remove Duplicates from Sorted Array差不多思路,只不过一个是删除重复值,一个是删除定值

既然不能申明额外的数组,那只能在原来的数组上做变动

变动前:[1,1,2,3,3],定值为:1

变动后:[2,3,3,3,3]

前3个值[2,3,3]就是我们所需要的

代码:

class Solution {
public int removeElement(int[] nums, int val) {
if(nums == null || nums.length == 0) return 0;
int j = 0;
for(int i = 0;i<nums.length;i++){
if(nums[i] != val){
nums[j] = nums[i];
System.out.println(nums[j]);
j++;
}
}
return j;
}
}

因为最后一次进入if(nums[i] != val)判断后,还是执行了一次j++,所以j的值就已经是数组长度了,return的时候不需要再+1了

欢迎关注我的微信公众号:安卓圈

【LeetCode算法-27】Remove Element的更多相关文章

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

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

  2. 【算法】LeetCode算法题-Remove Element

    这是悦乐书的第150次更新,第152篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第9题(顺位题号是27).给定整数数组nums和值val,删除nums中所有的val值, ...

  3. 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  ...

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

  5. 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 ...

  6. 【LeetCode】27 - Remove Element

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

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

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

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

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

  9. LeetCode OJ 27. Remove Element

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

  10. LeetCode:27. Remove Element(Easy)

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

随机推荐

  1. mysql终止当前正在执行的sql语句

    show processlist;找出你要停止的语句然后找出你要终止的语句的idkill 248

  2. 解决Invalid character found in the request target. The valid characters are defined in RFC 7230 and RF

    通过这里的回答,我们可以知道: Tomcat在 7.0.73, 8.0.39, 8.5.7 版本后,添加了对于http头的验证. 具体来说,就是添加了些规则去限制HTTP头的规范性 参考这里 具体来说 ...

  3. Zabbix导入MySQL数据库报错ERROR 1046 (3D000) at line 1: No database selected

    使用如下命令导入Zabbix数据库时报错 解决办法: 1.先把原始的数据库压缩包备份 cd /usr/share/doc/zabbix-server-mysql-4.0.7/ cp create.sq ...

  4. hive 初始化数据库报错

    安装hive,初始化数据库的时候报错 schematool -dbType mysql -initSchema Metastore Connection Driver : com.mysql.cj.j ...

  5. 独角兽估值30亿美金,我们聊聊RPA是什么

    https://www.jianshu.com/p/397ecd238ffc 缩短法定工作时间,已成国际劳动立法趋势,全球政府都曾面对这样的议题,过往企业IT也在思考这件事,开发出更好的软件系统帮助员 ...

  6. python完成数组格式的请求参数的加密计算

    #输入 '''order_id:31489 join_course[0][join_tel]:13130999882 join_course[0][join_name]:任学雨 join_course ...

  7. 视觉跟踪:MDnet

    应用需注明原创! 深度学习在2015年中左右基本已经占据了计算机视觉领域中大部分分支,如图像分类.物体检测等等,但迟迟没有视觉跟踪工作公布,2015年底便出现了一篇叫MDNet的论文,致力于用神经网络 ...

  8. 微信小程序——通讯录

    WXML: <view class="flex box box-lr"> <scroll-view class="flex groups box box ...

  9. Java项目使用 Tomcat 部署 Linux 服务器

    一.安装 Java 环境 1 下载 jdk 8 yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel 设置 centos7 环境变量 v ...

  10. etcd增删改查

    ![image-20191209191301618](C:\Users\AneroKissinger\AppData\Roaming\Typora\typora-user-images\image-2 ...