这是悦乐书的第150次更新,第152篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第9题(顺位题号是27)。给定整数数组nums和值val,删除nums中所有的val值,元素顺序可以改变,返回删除val值后数组的长度。不能使用新数组接收数据。例如:

给定数组nums = {3,2,2,3}, val = 3

你的函数应返回length = 2

其中nums的前两个元素为2

给定数组nums = [0,1,2,2,3,0,4,2],val = 2

你的函数应该返回length = 5

其中nums的前五个元素包含0,1,3,0和4

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

顺位比较并替换重复值。获取第i个元素,与val比较是否相等,如果相等,则判断val在此是否为连续出现,并使用j来记录索引,直到val不再连续出现的时候,将val第一次出现的值与索引j所代表的值进行互换,依次往后循环。内层嵌套了while循环,但是并不会每次都循环n-1次,其内部也有终止语句。

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

03 第二种解法

从索引0开始重新设值。定义初始条件count=-1,获取第i位元素与val比较,如果不相等,则nums[++count] = nums[i],此处使用前加加,直接将重复元素用后面的值替换点,而不是像第一种方法那样交换值。如果不习惯看前加加,还有后加加的写法。原则就是count会自然从0往后增加,第i位元素如果等于val,会继续循环,直到不等于,而count处于等待状态,等待非重复元素继续设值。

public int removeElement2(int[] nums , int val) {
int count = -1;
if(nums.length ==1 && nums[0]!=val)
return 1; for ( int n : nums){
if (n != val && count <= nums.length-2) {
nums[++count] = n;
}
}
return count+1;
} // 后加加的写法
public int removeElement3(int[] nums , int val) {
int count = 0;
if(nums.length ==1 && nums[0]!=val){
return 1;
}
for (int i=0; i<nums.length; i++) {
if (nums[i] != val && count <= nums.length-1) {
nums[count++] = nums[i];
}
}
return count;
}

04 小结

此题比较简单,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

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

  1. 乘风破浪:LeetCode真题_027_Remove Element

    乘风破浪:LeetCode真题_027_Remove Element 一.前言 这次是从数组中找到一个元素,然后移除该元素的所有结果,并且返回长度. 二.Remove Element 2.1 问题 2 ...

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

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

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

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

  5. [算法题] Remove Element

    题目内容 本题来源:LeetCode Given an array and a value, remove all instances of that value in place and retur ...

  6. leetcode第25题--Remove Element

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

  7. (算法)LeetCode刷题

    LeetCode 56 合并区别 Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 关键就是a[1]>=b[0] 也就 ...

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

  9. 【LeetCode】27 - Remove Element

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

随机推荐

  1. JS_单个或多个文件上传_不支持单独修改

    A-From表单直接填写提交地址,不过干预: 1. 单文件上传 最简单的文件上传,是单文件上传,form标签中加入enctype="multipart/form-data",for ...

  2. C#文件操作。

    文件操作常用相关类: File: 操作文件,静态类,对文件整体操作. Directory:操作目录(文件夹),静态类. FileInfo:文件类,用来描述一个文件对象,获取指定目录下的所有文件时,返回 ...

  3. Android Studio 使用Menu

    首先在res目录下创建一个文件夹名字随意 在对创建的文件夹下在创建一个菜单 名字随意 参看布局 可以看到你的菜单 可以选择添加是么样的菜单 接着要到主活动中重写 onCreateOptionsMenu ...

  4. SpringBoot数据库集成-Mybatis

    一.java web开发环境搭建 网上有很多教程,参考教程:http://www.cnblogs.com/Leo_wl/p/4752875.html 二.Spring boot搭建 1.Intelli ...

  5. python面向对象学习(二)基本语法

    目录 1. dir内置函数 2. 定义简单的类(只包含方法) 2.1 定义只包含方法的类 2.2 创建对象 2.3 编写第一个面向对象程序 3. 方法中的self参数 3.1 案例改造 -- 给对象添 ...

  6. ConstraintLayout使用

    引言 ConstraintLayout是一个ViewGroup,允许您以灵活的方式定位和调整小部件的方法,项目中的布局嵌套问题对项目性能有着不小的威胁,布局能实现扁平化的话会让软件性能得到很大的提升, ...

  7. angular 1.2.29版本下 动态添加多个表单、 校验全部、 提交 、ng-form方案

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. JS之onunload、onbeforeunload事件详解

    简介 onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过 window.onunload来调用.区别在于onbeforeunload在o ...

  9. SAP MM 物料主数据MRP2 视图Rounding Value字段

    SAP MM 物料主数据MRP2 视图Rounding Value字段 如下物料号,MRP2视图中,维护了rounding value字段值为50. MRP type :PD Lot size:EX ...

  10. IDEA项目搭建十三——服务消费端与生产端通信实现

    一.简介 之前已经完成了EurekaClient的服务生产者和Feign的服务消费者模块的搭建,现在实现统一的通信约定 (1) 统一Request结构 (2) 统一Response结构 (3) 统一E ...