【LeetCode算法-27】Remove Element
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的更多相关文章
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 【算法】LeetCode算法题-Remove Element
这是悦乐书的第150次更新,第152篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第9题(顺位题号是27).给定整数数组nums和值val,删除nums中所有的val值, ...
- 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 ...
- 【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 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 ...
- 【LeetCode】27. Remove Element 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...
- 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 ...
- LeetCode:27. Remove Element(Easy)
1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...
随机推荐
- 27.centos7基础学习与积累-013-文件和目录的权限
从头开始积累centos7系统运用 大牛博客: https://blog.51cto.com/yangrong/p5 https://blog.oldboyedu.com/ 文件的权限 rw-r--r ...
- ansible中的docker_container模块
docker_container模块 1.docker_container模块主要是用于ansible-playbook操作docker容器的一个模块,使用该模块可以实现批量创建docker容器 An ...
- go语言笔记2
上接Go语言学习笔记(一) 11 Go错误处理11.1 nil函数通常在最后的返回值中返回错误信息.使用errors.New 可返回一个错误信息: package main ...
- 如何将scratch3.0的作业自动提交到后台数据库
大家都知道Scratch3.0开发后,默认是可以下载文件到电脑,但是如果是作为商业系统来说,我们需要将作业自动的提交到后台,因此有了这篇文章. 首先,我们来分解下开发步骤: 1.在菜单栏新增一个上传到 ...
- 开发环境搭建之springboot+tk.mybatis整合使用逆向工程
一,引入xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorCo ...
- test20190904 JKlover
100+100+100=300.最后十分钟极限翻盘. 树链剖分 给一棵以1为根的有根树,开始只有1有标记. 每次操作可以给某个点打上标记,或者询问从某个点开始向上跳,遇到的第一个有标记的点. 对于 1 ...
- MySql数据封装操作类
1.先引用MySQL的DLL文件 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- [51 Nod 1584] 加权约数和
题意 求∑i=1N∑j=1Nmax(i,j)⋅σ1(ij)\large \sum_{i=1}^N\sum_{j=1}^Nmax(i,j)\cdot\sigma_1(ij)i=1∑Nj=1∑Nmax ...
- gj的zabbix客户端开机自启动设置
查看是否自启动 配置chkconfig 启动服务
- C Primer Plus AND 菜鸟教程
C语言概述 首先,windows 环境下安装 GCC编译环境 下载 MinGW 下载地址:http://sourceforge.net/projects/mingw/files/ 根据系统环境下载对应 ...