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. 解决web网站被挂马清除方法

    案例:某公司一个lamp的服务器网站站点目录下所有文件均被植入了广告脚本如下内容: <script language=javascriptsrc=http://%4%66E%78%72%67%2 ...

  2. 第二次作业 单例模式的SessionFactory以及线程安全的session

    单例模式的SessionFactory 在这个工具类Hibernate.java中写一个通过静态代码块生成唯一的SessionFactory,通过一个方法返回一个SessionFactory impo ...

  3. 脱壳_00_压缩壳_ASPACK

    写在前面的话: Aspack是最常见的一种压缩壳,具有较好的兼容性.压缩率和稳定性,今天我们就来一起分析一下这个壳: 零.分析压缩壳: 0.在开始动态调试前,用PEID和LoadPE查看一些信息,做到 ...

  4. java调用Linux执行Python爬虫,并将数据存储到elasticsearch--(环境脚本搭建)

    java调用Linux执行Python爬虫,并将数据存储到elasticsearch中 一.以下博客代码使用的开发工具及环境如下: 1.idea: 2.jdk:1.8 3.elasticsearch: ...

  5. Alpha冲刺报告(4/12)(麻瓜制造者)

    今日完成的情况 江郑: 今天对数据库的需求部分进行了完善 邓弘立: 完成了首页界面UI 刘双玉: 基本完成商品信息发布接口 汪志彬: 尝试UI的设计 符天愉: 将登录接口部署到服务器上,结果Linux ...

  6. Learn Algorithms With Javascript - 基于 Js 进行算法学习

    基于 javascript 学习并实现常用的经典算法,欢迎对算法和数学感兴趣的 Js 开发者参与,一起学习共同进步. 算法实现 排序 插入排序 sort/lib/insertion-sort.js 希 ...

  7. python reload(sys)找不到,name 'reload' is not defined和Python3异常-AttributeError: module 'sys' has no att

    基于python3.6.1版本,在一个.py文件中,加入这3行:import requests, re, sysreload(sys)sys.setdefaultencoding("utf- ...

  8. swift protocol的几种形式

    三个关注点:1.形式:2.实现方式:3.使用方式: 一.基本形式: 形式:内部无泛型类型: 实现:只需指定类型和实现相应的功能即可: 使用:可以用在其他类型出现的任何地方: protocol Resp ...

  9. 借助强大的IDEA开发ide高效实现equals,hashcode以及toString方法

    IDEA工具提供多种生成hashCode与equals的代码方案,注意:尽量不要使用第一个方案,第一个方案对于null不做判空处理,容易NNP问题. 对于生成toString方法方案,默认使用的是+拼 ...

  10. oracle常见受权与回收权限 grant和revoke

    1.GRANT 赋于权限 常用的系统权限集合有以下三个: CONNECT(基本的连接),   RESOURCE(程序开发),   DBA(数据库管理) 常用的数据对象权限有以下五个: ALL   ON ...