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 ...
随机推荐
- ODS设计
1.数据调研 2.确定数据范围 需要把上端应用需求与ODS数据范围进行验证,以确保应用所需的数据都已经从业务系统中抽取出来,并且得到了很好的组织,以ER模型表示数据主题关系 3.根据数据范围进行进一步 ...
- ConstraintLayout使用手册
1. 解决痛点 主要用拖拽 解决嵌套过多 2. 简易使用手册 增加约束 四个角直接拖拽就好了 删除约束 match_constraint 属性 这个属性类似于match_parent,去掉margin ...
- windows 2012 抓明文密码方法
windows 2012 抓明文密码方法 默认配置是抓不到明文密码了,神器mimikatz显示Password为null Authentication Id : 0 ; 121279 (0000000 ...
- [Python]运算符的优先级顺序
运算符 描述 ** 指数 (最高优先级) ~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@) * / % // 乘,除,取模和取整除 + - 加法减法 >> & ...
- FreeChart柱状图中如何取消柱子的倒影
JFreeChart柱状图中如何取消柱子的倒影,让柱子显示为一个平面图 Render 该怎么设置呢? 问题补充:已解决 intervalBarRender.setShadowVisible(false ...
- vagrant up下载box慢的解决办法
即在运行vagrant up时得到其的下载路径,如: https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20190101.0.0/prov ...
- Tornado框架实现异步爬虫
from urllib.parse import urljoin from bs4 import BeautifulSoup from tornado import gen, httpclient, ...
- nginx配置收集
同个服务,分别读取不同借口 location /xibao/service_api/ { if ($request_uri ~ ^/xibao/(.*)) { set $xibao_data_url ...
- 【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。
下面程序演示了在嵌入式Linux和PC机Linux下使用popen函数时,程序的运行结果是有差异的. 两个程序 atest.c 和 btest.c,atest 检查是否有 btest 进程运行,如果没 ...
- Xcode 备忘
一. 打印一堆乱七八糟的东西: Edit Scheme... --> Run --> Arguments,在 Environment Variables 里添加 OS_ACTIVITY_M ...