Description

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

The order of elements can be changed, and the elements after the new length don't matter.

Example

Given an array [0,4,4,0,0,2,4,4]value=4

return 4 and front four elements of the array is [0,0,0,2]

解题:今天这个题目还是挺简单的,给定一个数组,再给定一个值,要求把数组中里存在该值的,都剔除掉。在原数组的基础上,定一个rear作为待插入位置的下标,从0开始。遍历数组,如果不是value,放在rear位置,如果是,则啥也不做,直到循环结束。最后返回rear值,即除去value的元素个数。代码如下:

 public class Solution {
/*
* @param A: A list of integers
* @param elem: An integer
* @return: The new length after remove
*/
public int removeElement(int[] A, int elem) {
// write your code here
int rear = 0;
for(int i = 0; i < A.length; i++){
if(A[i] != elem){
A[rear++] = A[i];
}
}
return rear;
}
}

172. Remove Element【LintCode by java】的更多相关文章

  1. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  2. 212. Space Replacement【LintCode by java】

    Description Write a method to replace all spaces in a string with %20. The string is given in a char ...

  3. 165. Merge Two Sorted Lists【LintCode by java】

    Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...

  4. 158. Valid Anagram【LintCode by java】

    Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...

  5. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  6. 173. Insertion Sort List【LintCode by java】

    Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...

  7. 30. Insert Interval【LintCode by java】

    Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...

  8. 155. Minimum Depth of Binary Tree【LintCode by java】

    Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al ...

  9. 211. String Permutation【LintCode by java】

    Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...

随机推荐

  1. [IDEA_2] IDEA 问题合集

    1. IDEA 通过 Maven 导入的依赖包下面存在红色波浪线 问题描述: 创建的 Maven Project 在添加相关依赖后自动下载,自动添加的依赖包的下面存在红色波浪线,在使用过程中存在问题, ...

  2. Centos7(Firewall)防火墙开启常见端口命令

    Centos7默认安装了firewalld,如果没有安装的话,则需要YUM命令安装:firewalld真的用不习惯,与之前的iptable防火墙区别太大,但毕竟是未来主流讲究慢慢磨合它的设置规则: 安 ...

  3. Qt在控件未显示时如何获取正确的控件尺寸

    因为打算全屏显示一个对话框,而对话框内有几个QLabel的尺寸要在确定QLabel可用的最大尺寸后,再根据内容调整一次,所以在对话框构造函数内就想确定QLabel的最大尺寸,但因为QWidget::u ...

  4. Tidb进行缩减扩容tikv节点

    这两天接到任务说是要进行测试缩减机器给集群带来的负面效果有哪些. 然后我就按照官方的教程将机器进行了缩减,主要是缩减tikv节点 我们先来看看官方的文章是怎么写的: 步骤都没有什么问题,就是进行到第二 ...

  5. NoSQL数据库的认识

    SQL数据库和NoSQL数据库介绍 什么是SQL数据库? 关系型数据库是依据关系模型来创建的数据库.而所谓的关系模型就是“一对一.一对多.多对多”等关系模型,这是一种二维表格模型,因此一个关系型数据库 ...

  6. windows中使用git和开源中国

    现学现卖,学了忘忘了学. 非常感谢OSC提供了这么好的一个国内的免费的git托管平台.这里简单说下TortoiseGit操作的流程.很傻瓜了首先你要准备两个软件,分别是msysgit和tortoise ...

  7. FZU Monthly-201901 tutorial

    FZU Monthly-201901 tutorial 题目(难度递增) easy easy-medium medium medium-hard hard 思维难度 AHG F B CE D 编码难度 ...

  8. jdk1.7环境配置

    JDK1.7的环境配置(我的是jdk1.7,文件名写快了,忽略忽略) 官网下载自己需要的版本(ps:我这是朋友发给我的就不提供官网地址,去百度搜jdk就可以了) 下载下来除了改存放路径还有记得再jdk ...

  9. 【转】PHP中file_put_contents追加和换行

    在PHP的一些应用中需要写日志或者记录一些信息,这样的话. 可以使用fopen(),fwrite()以及 fclose()这些进行操作. 也可以简单的使用file_get_contents()和fil ...

  10. ICSharpCode.SharpZipLib 开源压缩库使用示例

    官方网站:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 插件描述: ICSharpCode.SharpZipLib.dl ...