本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41577997

Remove Element

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

思路:

(1)这道题很简单。由于没有空间上的限制,很容易解决。

(2)本文的方法是创建一个链表,将和指定数字不同的元素放入链表,最后得到的链表长度即为所求长度。

(3)为了让移除后剩余的数字在默认顺序上,还必须还原数组的顺序,这里直接遍历链表,将其中元素对应到数组中。

(4)本文只是给出解题方法,由于技术有限,效率和空间的优化尚未涉及,对于大神来说,本文的算法就显得很垃圾了,不过也希望对你有所帮助。

算法所对应代码如下所示:

public static int removeElement(int[] A, int elem) {
	int len = A.length;
	if (len == 0)
		return 0;
	List<Integer> list = new LinkedList<Integer>();

	for (int i = 0; i < len; i++) {
		if (A[i] != elem) {
		   list.add(A[i]);
		}
	}

	for (int i = 0; i < list.size(); i++) {
		A[i] = list.get(i);
	}

	return list.size();
}

上题的算法只是针对数字,如果改为任意对象,那么我们在判断时,就不能用==来进行判断了,而是用equals()方法来进行值的判断。

从对象数组中移除值相同的指定对象并返回剩余对象个数的算法如下:

public static int removeObject(Object[] A, Object elem) {
	int len = A.length;
	if (len == 0)
		return 0;
	List<Object> list = new LinkedList<Object>();

	for (int i = 0; i < len; i++) {
		if (A[i].equals(elem)) {
			list.add(A[i]);
		}
	}

	A = new Object[list.size()];
	for (int i = 0; i < list.size(); i++) {
		A[i] = list.get(i);
	}

	return list.size();
}

Leetcode_27_Remove Element的更多相关文章

  1. Spring配置文件标签报错:The prefix "XXX" for element "XXX:XXX" is not bound. .

    例如:The prefix "context" for element "context:annotation-config" is not bound. 这种 ...

  2. 【解决方案】cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param'. One of '{"http://java.sun.com/xml/ns/javaee":run-as, "http://java.sun.com/xml/ns/javaee":security-role-r

    [JAVA错误] cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param'. One o ...

  3. WebComponent魔法堂:深究Custom Element 之 从过去看现在

    前言  说起Custom Element那必然会想起那个相似而又以失败告终的HTML Component.HTML Component是在IE5开始引入的新技术,用于对原生元素作功能"增强& ...

  4. WebComponent魔法堂:深究Custom Element 之 标准构建

    前言  通过<WebComponent魔法堂:深究Custom Element 之 面向痛点编程>,我们明白到其实Custom Element并不是什么新东西,我们甚至可以在IE5.5上定 ...

  5. WebComponent魔法堂:深究Custom Element 之 面向痛点编程

    前言  最近加入到新项目组负责前端技术预研和选型,一直偏向于以Polymer为代表的WebComponent技术线,于是查阅各类资料想说服老大向这方面靠,最后得到的结果是:"资料99%是英语 ...

  6. 深入理解DOM节点类型第五篇——元素节点Element

    × 目录 [1]特征 [2]子节点 [3]特性操作[4]attributes 前面的话 元素节点Element非常常用,是DOM文档树的主要节点:元素节点是html标签元素的DOM化结果.元素节点主要 ...

  7. cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.

    spring 配置文件报错报错信息:cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be ...

  8. MongoDB查询转对象是出错Element '_id' does not match any field or property of class

    MongoDB查询转对象是出错Element '_id' does not match any field or property of class   解决方法: 1.在实体类加:[BsonIgno ...

  9. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

随机推荐

  1. Java8 按照类属性去重

    测试po package com.shiwulian.test.po; public class Person {private String id;private String name;priva ...

  2. 创建OpenGL Context(WGL)

    创建OpenGL Context(WGL) 创建OpenGL Context是初始化OpenGL的一部分.只有在此之后才能使用OpenGL. 关于platform的注意事项 创建OpenGL cont ...

  3. python中的函数(定义、多个返回值、默认参数、参数组)

    函数定义 在python中函数的定义以及调用如下代码所示: def test(x): y = x+1 return y result = test(2) print(result) 多个返回值的情况 ...

  4. python笔记十二(匿名函数)

    一.匿名函数 有些情况下,我们需要把函数当做参数传入到另外的函数中,或者是把函数作为某个函数的返回值,此时我们就可以使用匿名函数. 匿名函数的标志是lambda.   >>> f = ...

  5. CMCC验证绕过POC

    大学的时候无意间发现绕过CMCC验证的方法(贫穷使人进步...),写了段POC脚本,时过两年,漏洞应该已经失效了(我猜 --),刚刚发现有人私信问我要,都那么久了鬼还记得写的什么啊,但确实看到了又不能 ...

  6. java中的final和volatile详解

    相比synchronized,final和volatile也是经常使用的关键字,下面聊一聊这两个关键字的使用和实现 1.使用 final使用: 修饰类表示该类为终态类,无法被继承 修饰方法表示该方法无 ...

  7. 解决ASP.NET MVC 检测到有潜在危险的 Request.Form 值

    提交使用html编辑器编辑后的数据,由于Request时出现有HTML或JavaScript等字符串时,系统会认为是危险性值.立马报错. "从客户端 ... 中检测到有潜在危险的 Reque ...

  8. Java集合框架总结

    java集合框架主要分为实现了Collection接口的List和Set.映射接口Map. |-- List 有序,元素都有索引,可重复. |-- Set 无序,不可以存储重复的元素. |-- Map ...

  9. Redis监控工具,命令和调优

    Redis监控工具,命令和调优 1.图形化监控 因为要对Redis做性能测试,发现了GitHub上有个python写的RedisLive监控工具评价不错.结果鼓捣了半天,最后发现其主页中引用了Goog ...

  10. SQL批处理与事务控制

    今天我想要分享的是关于数据库的批处理与事务的控制.批处理对于项目的实际应用有非常大的具体意义. 一.批处理部分 首先我们新建一个表: create table t3( id int primary k ...