本文是在学习中的总结,欢迎转载但请注明出处: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. Web压力测试和手机App测试

    总纲:认识测试关系和目标http://blog.csdn.net/superxgl/article/details/27189631 一.web测试和App服务端测试 软件安装 建议安装loadrun ...

  2. SpringMVC格式转化错误之HTTP Status [400] – [Bad Request]

    SpringMVC中,如果直接为Date类型的属性赋值,服务器有可能会报HTTP Status [400] – [Bad Request] Type Status Report Description ...

  3. django之允许外部机器访问

    开开启django时,使用0.0.0.0:xxxx,作为ip和端口例如: python3 manage.py runserver 0.0.0.0:9000 然后在settings里修改ALLOWED_ ...

  4. admin的配置

    当我们访问http://127.0.0.1:8080/admin/时,会出现: 执行命令: 生成同步数据库的脚本:python manage.py makemigrations             ...

  5. 简单将sublime text 配置为lua或c#一键编译运行环境

    lua { "cmd": "luajit $file", "selector":"source.lua" } C { & ...

  6. 安卓高级5 zXing

    ZXing作者的github地址: https://github.com/zxing/zxing 这里为大家也提供一个封装好的最新的ZXing Lib: https://github.com/xuyi ...

  7. 拾遗与填坑《深度探索C++对象模型》3.3节

    <深度探索C++对象模型>是一本好书,该书作者也是<C++ Primer>的作者,一位绝对的C++大师.诚然该书中也有多多少少的错误一直为人所诟病,但这仍然不妨碍称其为一本好书 ...

  8. Dynamics CRM 构建IN查询

    CRM中有3种查询数据的方式,分别是QueryExpression.fetchxml.linq,本篇讲述的条件IN的查询只支持前两种,linq并不支持. QueryExpression的写法如下,示例 ...

  9. 前端CSS技术全解(一)

    一.概述 1)用HTML完成样式工作 哪个标签有哪个属性难以记忆 需求变更影响较大(例如像修改成功法则以下的文字颜色需要修改4个地方) <h1 align="center"& ...

  10. EBS开发技术之trace

    trace的目的 trace主要是用于程序调优,优化,程序bug调试,程序运行系统情况跟踪 trace步骤 1.并发定义中,勾上"启用跟踪" 2.提交一个请求,得到请求编号 注意: ...