172. Remove Element【LintCode by java】
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】的更多相关文章
- 156. Merge Intervals【LintCode by java】
Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...
- 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 ...
- 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 ...
- 158. Valid Anagram【LintCode by java】
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...
- 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 ...
- 173. Insertion Sort List【LintCode by java】
Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...
- 30. Insert Interval【LintCode by java】
Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...
- 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 ...
- 211. String Permutation【LintCode by java】
Description Given two strings, write a method to decide if one is a permutation of the other. Exampl ...
随机推荐
- python Pipe 双管道通信
管道:是python多进程中一种交换数据的方式 from multiprocessing import Process,current_process,Queue,Pipe import time i ...
- Java-控制台传递参数
今天组长叫我把所有的参数(写死的),用控制器输入,使其变成可变的. ------我的程序是需要读取文件的,控制台输入即,是文件放在哪我都可以读取. 比如我需要读取的demo.txt文件在D盘根目录下, ...
- Hadoop 高可用(HA)的自动容灾配置
参考链接 Hadoop 完全分布式安装 ZooKeeper 集群的安装部署 0. 说明 在 Hadoop 完全分布式安装 & ZooKeeper 集群的安装部署的基础之上进行 Hadoop 高 ...
- 一、JSP九大内置对象 二、JAVAEE三层架构和MVC设计模式 三、Ajax
一.JSP九大内置对象###<1>概念 不需要预先申明和定义,可以直接在jsp代码中直接使用 在JSP转换成Servlet之后,九大对象在Servlet中的service方法中对其进行定义 ...
- 第 14 章 结构和其他数据形式(names)
*--------------------------------- names1.c -- 使用指向结构的指针 ---------------------------------*/ #includ ...
- Oracle物化视图的创建及使用(一
Oracle物化视图的创建及使用 http://blog.csdn.net/tegwy/article/details/8935058 先看简单创建语句: create materialized ...
- 将Vue-cli搭建的项目改造成多页面应用时对项目结构和配置的调整
创建项目 首先初始化一个Vue项目模板,之后在模板下载时候会弹出如下配置选项 vue init webpack demo 配置好后按下回车就构建完成了Vue脚手架,之后cd进入项目,并且进行node模 ...
- iosclient发现_世界杯送流量活动项目总结
世界杯如火如荼的进行.视频站点相似于门户站点.须要高速依据外部环境更新内容. 产品经理须要策划活动,并安排实施.这个活动就是在这样背景下产生的,爱奇艺与运营商合作,实现双赢.爱奇艺能够通过运营商 ...
- gcd以及exgcd入门讲解
gcd就是最大公约数,gcd(x, y)一般用(x, y)表示.与此相对的是lcm,最小公倍数,lcm(x, y)一般用[x, y]表示. 人人都知道:lcm(x, y) = x * y / gcd( ...
- oracle11g dataguard 备库数据同步的检查方法
概述: 一.环境 主库: ip地址:192.168.122.203 oracle根目录:/data/db/oracle SID:qyq 数据文 ...