这是悦乐书的第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. SpringBoot学习(六)-->SpringBoot的自动配置的原理

    Spring Boot的自动配置的原理 Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入 ...

  2. linux内核源码目录结构分析

    原文地址 /arch.arch是architecture的缩写.arch目录下是好多个不同架构的CPU的子目录,譬如arm这种cpu的所有文件都在arch/arm目录下,X86的CPU的所有文件都在a ...

  3. 使用HttpWebRequest请求API接口以及其他网站资源

    很多时候,我们项目需要其他网站的资源,而这个被请求的网站可能属于你们自己开发管理的网站.也可能是公网上其他网站对外开发的API接口,比如说腾讯的微信公众平台的API接口.各大短信服务商的短信API接口 ...

  4. iframe关闭操作

    关闭自定义 Div+Iframe弹窗 :window.parent.$("div的id/class/name").remove();//移除div 关闭Iframe弹窗:windo ...

  5. C++程序实例唯一方案,窗口只打开一次,程序只打开一次

    首先是方法: // IsAlreadyRunning - 是否已经运行 BOOL IsAlreadyRunning() { BOOL bRet = FALSE; HANDLE hMutex = ::C ...

  6. [android] android消息机制入门

    上一节,先把访问网络的部分放到一个子线程里面去执行,new Thread(){}.start(),new Thread直接使用匿名内部类来实现,重写run()方法,内部类访问外部的变量,这个变量应该定 ...

  7. JSJ—编译器与虚拟机哪个重要?

    阅读本文约“2分钟” 熟悉Java的朋友都知道虚拟机还有编译器,那么它们各自主要的功能是什么?谁比较重要呢?让我们来了解一下这两位美女的故事. 虚拟机可以说就是Java,她能让程序运行起来. 但是编译 ...

  8. 大家好,又是新的一天。今天给大家带来一些新的知识:选择器的种类和css的三种样式

    今天我们为大家 选择了 六种 选择器: 1. 标签选择器 2.id选择器 3.class选择器 4.后代选择器 5.子代选择器 6.交集选择器 我们就举三个典型的例子:后代选择器,子代选择器和交集选择 ...

  9. float浮动的世界

    loat有四个属性,分别是: float:none:  没有浮动: float:left:  左浮动: float:right: 右浮动: float:inherit:继承父元素的浮动: ------ ...

  10. cf438E. The Child and Binary Tree(生成函数 多项式开根 多项式求逆)

    题意 链接 Sol 生成函数博大精深Orz 我们设\(f(i)\)表示权值为\(i\)的二叉树数量,转移的时候可以枚举一下根节点 \(f(n) = \sum_{w \in C_1 \dots C_n} ...