【算法】LeetCode算法题-Remove Element
这是悦乐书的第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的更多相关文章
- 乘风破浪:LeetCode真题_027_Remove Element
乘风破浪:LeetCode真题_027_Remove Element 一.前言 这次是从数组中找到一个元素,然后移除该元素的所有结果,并且返回长度. 二.Remove Element 2.1 问题 2 ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 【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 ...
- 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 ...
- [算法题] Remove Element
题目内容 本题来源:LeetCode Given an array and a value, remove all instances of that value in place and retur ...
- leetcode第25题--Remove Element
problem: Given an array and a value, remove all instances of that value in place and return the new ...
- (算法)LeetCode刷题
LeetCode 56 合并区别 Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 关键就是a[1]>=b[0] 也就 ...
- 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 ...
随机推荐
- springBoot系列-->springBoot注解大全
一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
- C#基础知识总结(七)
摘要 C#最简单的两个算法:输入数字打印出菱形星星.冒泡排序1.输入数字打印出菱形星星 static void Main(string[] args) { //输入一个数,表示菱形星号,数字必须是一个 ...
- 从零开始学安全(八)●Ubuntu 16 LAMP环境搭建
1.Apache2 web 服务器的安装 : 可以先更新一下服务器 sudo apt-get update # 获取最新资源包sudo apt-get upgrade # 本机软件全部更新sudo a ...
- php 函数小技巧(一)
密码加密与验证 password_hash — 创建密码的哈希(hash) string password_hash ( string $password , integer $algo [, arr ...
- mybatis整合springboot 以及需要注意的细节
具体怎么整合的网上有很多优秀的博客介绍,这里就直接引用一篇个人觉得非常详细的教程: https://blog.csdn.net/winter_chen001/article/details/77249 ...
- Go实现基于WebSocket的弹幕服务
拉模式和推模式 拉模式 1.数据更新频率低,则大多数请求是无效的 2.在线用户量多,则服务端的查询负载高 3.定时轮询拉取,实时性低 推模式 1.仅在数据更新时才需要推送 2.需要维护大量的在线长连接 ...
- cas单点登录
一般一家企业内部会有多个业务系统,多个系统相互协作,但每个系统都需要登陆密码,比如:权限系统,发布系统,配置系统,邮件系统等,用户使用每个系统都需要登陆操作,使用起来比较繁琐,工作效率低下.此时就需要 ...
- 汇编语言--微机CPU的指令系统(五)(条件设置字节指令)
(10)条件设置字节指令 条件设置字节指令(Set Byte Conditionally)是80386及其以后CPU所具有的一组指令.它们在测试条件方面与条件转移是一致的,但在功能方面,它们不是转移, ...
- #WEB安全基础:HTML/CSS | 0x0 我的第一个网页
#WEB安全基础:HTML/CSS系列,本系列采用第二人称以免你不知道我在对着你说话,以朋友的视角和你交流 HTML的中文名叫做超文本标记语言,CSS叫做层叠样式表 用HTML设计你的第一个网页,你需 ...
- ASPxGridView 用法
一.ASPxGridView属性:概述设置(Settings) 1.1.Settings <Settings GridLines="Vertical" : 网格样式 Vert ...