第18题 Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Solution1:
public class Solution {
    public int removeElement(int[] A, int elem) {
        int length=0, ptr=0;
        for(int i=0; i<A.length;i++){
            if(A[i]!=elem){
                A[ptr]=A[i];
                ptr++;
                length++;
            }
        }
        return length;
    }
}
Solution2:不要的用数组尾的数据填充,改变数组长度就可以。
public class Solution {
    public int removeElement(int[] A, int elem) {
        int length=A.length;
        for(int i=0; i<length;i++){
            if(A[i]==elem){
                A[i]=A[length-1];
                length--;
                i--;
            }
        }
        return length;
    }
}
第18题 Remove Element的更多相关文章
- [算法题] 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算法题-Remove Element
		
这是悦乐书的第150次更新,第152篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第9题(顺位题号是27).给定整数数组nums和值val,删除nums中所有的val值, ...
 - leetCode练题——27. Remove Element
		
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
 - [LeetCode] Remove Element 分析
		
Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...
 - 27. Remove Element【leetcode】
		
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
 - 乘风破浪:LeetCode真题_027_Remove Element
		
乘风破浪:LeetCode真题_027_Remove Element 一.前言 这次是从数组中找到一个元素,然后移除该元素的所有结果,并且返回长度. 二.Remove Element 2.1 问题 2 ...
 - leetCode 27.Remove Element (删除元素) 解题思路和方法
		
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
 - 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue
		
[源码下载] 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue 作者:weba ...
 
随机推荐
- 【NOIP 2002】 字串变换
			
[题目链接] https://www.luogu.org/problemnew/show/P1032 [算法] 广度优先搜索 用stl库里的map来判重 [代码] #include<bits/s ...
 - [转载]linux上安装oracle
			
原文地址:linux上安装oracle作者:天涯恨客 1.创建oinstall组 [root@xieqing ~]# groupadd oinstall 创建dba组 [root@xieqing ~] ...
 - 10分钟教你Python+MySQL数据库操作
			
欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 本文介绍如何利用python来对MySQL数据库进行操作,本文将主要从以下几个方面展开介绍: 1.数据库介绍 2.MySQL数据库安装和设置 ...
 - LeetCode Weekly Contest 27
			
1. 557. Reverse Words in a String III 分割字符串,翻转. class Solution { public: string reverseWords(string ...
 - WebForm--j简单控件、简单的登录(怎么链接数据库)
			
一.简单控件 1.label:边框(边框的颜色.样式.粗细) 是专门显示文字的, 被编译后是 <span id="Label1">Label</spa ...
 - JVM概论
			
引子 Java虚拟机是Java应用程序的执行环境.通常而言,JVM是由一组严格的指令集和一个复杂的内存模型来具体实现的虚拟机,它用来解释编译好的java字节码文件,将字节码转换为特定机器可以执行的本机 ...
 - ADODB.RecordSet常用方法查询
			
rs = Server.CreateObject("ADODB.RecordSet") rs.Open(sqlStr,conn,1,A) 注:A=1表示读取数据:A=3表示新增.修 ...
 - vue 脚手架 使用步骤
			
当我知道搭建脚手架得使用命令行的时候.我就崩溃了.所以写一篇记录以后留着自己用也方便大家. 首先要安装一个node 环境, 1.打开cmd 进到你要建项目的目录: E: ...
 - 前端手机验证码cookie存储
			
注册的时候经常会有手机验证码的输入这个环节,在第一次点击发送了验证码只后,比如倒计时只走了10秒钟,然后刷新的话,倒计时要还是存在的,这个时候就要有一个cookie的存在了. html的代码 < ...
 - java 常用API
			
package com.orcal.demc01; public class Regex { public static void main(String[] args) { // TODO Auto ...