Question

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.

Solution -- Two Pointers

Remember that p1 starts from -1.

 public class Solution {
public int removeElement(int[] nums, int val) {
int length = nums.length;
if (length == 0)
return 0;
if (length == 1) {
if (nums[0] == val)
return 0;
else
return 1;
}
int p1 = -1, p2 = 0;
while (p2 < length) {
if (nums[p2] != val) {
p1++;
nums[p1] = nums[p2];
p2++;
} else {
p2++;
}
}
return p1 + 1;
}
}

Remove Element 解答的更多相关文章

  1. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  2. [LeetCode] Remove Element 分析

    Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...

  3. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

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

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

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. [array] leetCode-27. Remove Element - Easy

    27. Remove Element - Easy descrition Given an array and a value, remove all instances of that value ...

  7. leetcode-algorithms-27 Remove Element

    leetcode-algorithms-27 Remove Element Given an array nums and a value val, remove all instances of t ...

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

  9. [LeetCode] Remove Element题解

    Remove Element: Given an array and a value, remove all instances of that value in place and return t ...

随机推荐

  1. 工具:BT Sync 同步文件

    随着互联网的发展,文件共享变得越来越便捷,但是文件的共享过程是不是安全,这一直是人们关心的问题,今天向大家介绍一个共享工具,可以实现便捷的安全共享.      这个工具分为两个部分,一个是服务器部分, ...

  2. poj 1017 Packets 贪心

    题意:所有货物的高度一样,且其底面积只有六种,分别为1*1 2*2 3*3 4*4 5*5 6*6的,货物的个数依次为p1,p2,p3,p4,p5,p6, 包裹的高度与货物一样,且底面积就为6*6,然 ...

  3. C#Http编程

    c# 模拟 网页实现12306登陆.自动刷票.自动抢票完全篇(转) 这一篇文章,我将从头到尾教大家使用c#模拟网页面登陆12306网站,自动刷票,选择订票人,到最后一步提交订单.研究过HTTP协议的童 ...

  4. 行人检測之HOG特征(Histograms of Oriented Gradients)

    之前的文章行人计数.计次提到HOG特征这个概念,这两天看了一下原版的论文,了解了一下HOG特征的原理,并依据自己的理解将这种方法的流程写了下来,假设有不正确的地方欢迎指正. HOG(Histogram ...

  5. hdu 4923 Room and Moor (单调栈+思维)

    题意: 给一个0和1组成的序列a,要构造一个相同长度的序列b.b要满足非严格单调,且 值为0到1的实数.最后使得  sum((ai-bi)^2)最小. 算法: 首先a序列開始的连续0和末尾的连续1是能 ...

  6. MFC获取当前时间

    获取按钮消息响应函数: void CTest17GetTimeDlg::OnGetTime() { // TODO: 在此添加控件通知处理程序代码 //UpdateData(true); CTime  ...

  7. 软件测试作业三 尝试使用JUnit

    写一个判断三角形种类的代码,对其进行测试. 判断三角形代码: package 测试1; public class sjx { public String f(int a,int b,int c) { ...

  8. div没有设置高度时背景颜色不显示(浮动)

    在使用div+css进行网页布局时,如果外部div有背景颜色或者边框,而不设置其高度,在IE浏览器下显示正常.但是使用Firefox/opera浏览时却出现最外层Div的背景颜色和边框不起作用的问题. ...

  9. java学习基础

    Q: What if the static modifier is removed from the signature of the main method? A: Program compiles ...

  10. OpenCV——像素数据类型总结<摘>

    1.Unsigned 8bits(一般的图像文件格式使用的大小)IplImage数据结构参数:IPL_DEPTH_8UCvMat数据结构参数:CV_8UC1,CV_8UC2,CV_8UC3,CV_8U ...