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的元素个数。代码如下:

 1 public class Solution {
 2     /*
 3      * @param A: A list of integers
 4      * @param elem: An integer
 5      * @return: The new length after remove
 6      */
 7     public int removeElement(int[] A, int elem) {
 8         // write your code here
 9         int rear = 0;
10         for(int i = 0; i < A.length; i++){
11             if(A[i] != elem){
12                 A[rear++] = A[i];
13             }
14         }
15         return rear;
16     }
17 }

原文地址:https://www.cnblogs.com/phdeblog/p/9141379.html

时间: 2024-11-12 08:54:36

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

156. Merge Intervals【LintCode by java】

Description Given a collection of intervals, merge all overlapping intervals. Example Given intervals => merged intervals: [ [ (1, 3), (1, 6), (2, 6), => (8, 10), (8, 10), (15, 18) (15, 18) ] ] Challenge O(n log n) time and O(1) extra space. 题意:给定一个

158. Valid Anagram【LintCode by java】

Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification What is Anagram? Two strings are anagram if they can be the same after change the order of characters. Example Given s = "abcd", t = "dcab&q

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 sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order. Example Given 1->3->8->11->15->null,

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 characters array, you can assume it has enough space for replacement and you are given the true length of the string. You code should also return the new

175. Invert Binary Tree【LintCode by java】

Description Invert a binary tree. Example 1 1 / \ / 2 3 => 3 2 / 4 4   解题:题目要求讲二叉树的左子树和右子树对调一下,用递归来做很简单: 1 /** 2 * Definition of TreeNode: 3 * public class TreeNode { 4 * public int val; 5 * public TreeNode left, right; 6 * public TreeNode(int val) {

181. Flip Bits【LintCode, by java】

Description Determine the number of bits required to flip if you want to convert integer n to integer m. Both n and m are 32-bit integers. Example Given n = 31 (11111), m = 14 (01110), return 2. 解题:比较两个整数对应的二进制数,共有多少位不同.注意,负数也包含在内.">>>"

27. Remove Element【easy】

27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be ch

MongDB基础学习(七)—— 【MongoDB for Java】Java操作MongoDB

[MongoDB for Java]Java操作MongoDB 开发的产品为了融资,不停得改版,从第一版到现在最新版本,最后发现公司发展方向都变了,有最初电子商务改成VR内容提供者(没办法,要别人钱,就得按照别人的规划的战略走).本来本章节会放到后面再做讲解,无奈,部门需要做一次培训任务,我就想到拿Java操作MongoDB作为培训内容,开发环境和依赖jar如下: (1)开发环境: System:Windows IDE:eclipse Database:mongoDB2.6 Maven:apac

Java编程思想【Thinking in java】

Java编程思想[Thinking in java]目录:第1章 对象导论1.1抽象过程1.2每个对象都有一个接口1.3每个对象都提供服务1.4被隐藏的具体实现1.5复用具体实现1.6继承1.6.1“是一个”(is-a)与“像是一个”(is-like-a)关系1.7伴随多态的可互换对象1.8单根继承结构1.9容器1.9.1参数化类型(范型)1.10对象的创建和生命周期1.11异常处理:处理错误1.12并发编程1.13Java与Internet1.13.1Web是什么1.13.2客户端编程1.13