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. oracl数据库中的substr()函数的用法

    substr:字符串截取. 1.substr:(字符串 | 列 ,开始点):从开始一直截取到结尾. select substr(zym,2) from bqh4 2.substr:(字符串 | 列 , ...

  2. Linux centos6.5 系统语言改成中文简体

    有时候上传的文件在linux上ls显示的时乱码,原因可能是系统语言编码问题,以Linux centos6.5为例,解决方法如下: 1.在root(皇帝)权限下更改: 查看当前所有语言环境:locale ...

  3. Matlab feval函数(转)

    http://zhidao.baidu.com/link?url=7CusQYQXhCDB8sUtolMEhI1ctnpblbYrpSnU0fhIh5LvDZuhsBuozQusS6Kb1McTp7x ...

  4. 记录一次nginx配置vhost的小bug

    话说这篇博客是在是为了保持自己记录生活的习惯而写的,没有什么阅读的价值,各位读者可以直接忽略了.今天在配置一个域名的时候,写了new_example.com(举例而已) 因为是内测,所以并未想象到深层 ...

  5. react的新手基础知识笔记

    <!DOCTYPE html> <html> <head> <script src="../build/react.js">< ...

  6. Spring Boot 集成 thymeleaf 模版引擎

    Spring Boot 建议使用 HTML 来完成动态页面.Spring Boot 提供了大量的模版引擎,包括 Thymeleaf.FreeMarker.Velocity等. Spring Boot ...

  7. mac brew安装mysql

    mac不自带mysql,这里需要重新安装,方法依然很简单 brew install mysql unset TMPDIR mysql_install_db --verbose --user=`whoa ...

  8. Netty入门(一)环境搭建及使用

    一.项目创建 在 Eclipse 中右键,新建->项目->Maven->Maven Project->下一步->选择 quickstart 下一步->设置如图(参数 ...

  9. ConsenSys/eth-lightwallet(browserless)

    https://github.com/ConsenSys/eth-lightwallet LightWallet A minimal ethereum javascript wallet.一个小型的钱 ...

  10. 11.C++和C的区别,什么是面向对象

    c++封装更好,调用接口,c调用子函数 1.首先C和C++在基础语句上没有太大区别,c++在c基础上改进,兼容大部分c的语法结构.c++面向对象,c面向过程. 2.新增new和delete的语法,引用 ...