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. HTML+Css+JavaScript知识点汇总

    HTML 部分 HTML基础知识 1. HTML简介 HTML(Hypertext Markup Language),超文本标记语言,HTML利用各种标记来标识文档的结构以及标识超链接的信息.它是从S ...

  2. HTML常用全部代码--第二部分--JAVASCRIPT( 小伙伴要牢记😁😁😁😁 )

    html代码大全--js部分:小技巧 (1) 点击关闭窗口<a href="javascript.:top.window.close();">点击关闭窗口</a& ...

  3. 编译heartbeat出现的问题

    如报 cc1: warnings being treated as errors pils.c:245: error: initialization fromincompatible pointer ...

  4. 移动平台前端开发总结(ios,Android)

    首先我们来看看webkit内核中的一些私有的meta标签,这些meta标签在开发webapp时起到非常重要的作用 <meta content="width=device-width; ...

  5. Codeforces Round #605 (Div. 3) E. Nearest Opposite Parity(最短路)

    链接: https://codeforces.com/contest/1272/problem/E 题意: You are given an array a consisting of n integ ...

  6. Spring之IOC(控制反转)与AOP(面向切面编程)

    控制反转——Spring通过一种称作控制反转(IoC)的技术促进了松耦合.当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象.可以认为IoC与JND ...

  7. idea中properties配置文件 注释显示中文乱码问题

  8. 持续集成学习11 jenkins和gitlab集成自动触发

    一.配置gitlab上提交代码后在jenkins上自动构建 1.在jenkins上配置gitlab 系统管理--->系统设置--->gitlab配置 2.在gitlab上配置token 3 ...

  9. learning scala akka ask_pattern

    package com.example import akka.actor._ import akka.util.Timeout object Tutorial_03_Ask_Pattern exte ...

  10. learning scala someElements

    The Scala collections library provides specialised implementations for Sets of fewer than 5 values ( ...