Leetcode_27_Remove Element
本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章
- Spring配置文件标签报错:The prefix "XXX" for element "XXX:XXX" is not bound. .
例如:The prefix "context" for element "context:annotation-config" is not bound. 这种 ...
- 【解决方案】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 ...
- WebComponent魔法堂:深究Custom Element 之 从过去看现在
前言 说起Custom Element那必然会想起那个相似而又以失败告终的HTML Component.HTML Component是在IE5开始引入的新技术,用于对原生元素作功能"增强& ...
- WebComponent魔法堂:深究Custom Element 之 标准构建
前言 通过<WebComponent魔法堂:深究Custom Element 之 面向痛点编程>,我们明白到其实Custom Element并不是什么新东西,我们甚至可以在IE5.5上定 ...
- WebComponent魔法堂:深究Custom Element 之 面向痛点编程
前言 最近加入到新项目组负责前端技术预研和选型,一直偏向于以Polymer为代表的WebComponent技术线,于是查阅各类资料想说服老大向这方面靠,最后得到的结果是:"资料99%是英语 ...
- 深入理解DOM节点类型第五篇——元素节点Element
× 目录 [1]特征 [2]子节点 [3]特性操作[4]attributes 前面的话 元素节点Element非常常用,是DOM文档树的主要节点:元素节点是html标签元素的DOM化结果.元素节点主要 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- 韩顺平玩转Oracle视频资料整理
.oracle10g 11g:g(grid)表示网格技术 以baidu搜索为准,现在想使用一个软件,但是此软件在离自己非常近的地方就存在了下载地址,但是与自己非常远的地方也同样存在一个下载地址,而搜索 ...
- Java不走弯路教程(2.Hello,Java!)
2.Hello,Java! 欢迎来到Java的世界,在上一章,我们已经完成了DOS的基本操作学习和Java的环境搭建,在本章中我们Java来完成一个简单的DOS程序. 2.1 Hello,Java! ...
- UltraISO安装centos7系统
1. 使用最新版UltraISO将ISO镜像刻录到U盘一定要是最新版,试用版都可以,按下图操作: 2. U盘启动电脑进入安装界面正常情况下你应该会看到下面的这个界面: 选择第一项,然后按TAB键(在评 ...
- 基于Java配置Spring加Hibernate和再加SpringData时的差别
先在类路径application.properties jdbc.driverClassName = org.postgresql.Driver jdbc.url = jdbc:postgresql: ...
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
在上篇博客初识Spring Boot框架中我们初步见识了SpringBoot的方便之处,很多小伙伴可能也会好奇这个Spring Boot是怎么实现自动配置的,那么今天我就带小伙伴我们自己来实现一个简单 ...
- 数学API Math.atan() 和Math.atan2() 三角函数复习
今天在学习贝塞尔曲线看到需要结合三角函数 以及两个不认识的Api :API Math.atan() 和Math.atan2() 先看下三角函数 正切函数图:(180为一个周期 即45=45+180) ...
- 全文检索Lucene (2)
接着全文检索Lucene (1) . 下面我们来深入的研究一下,如何使用Lucene! 从全文检索Lucene (1)中我们可以看出,Lucene就好比一个双向的工作流,一方面是对索引库的维护,另一方 ...
- Android必知必会-Stetho调试工具
一.背景 Stetho是 Facebook 出品的一个强大的 Android 调试工具,使用该工具你可以在 Chrome Developer Tools查看APP的布局, 网络请求(仅限使用Volle ...
- 【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- Error处理:Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack tra
[2014-04-20 20:59:23 - MyDetectActivity] Dx trouble writing output: already prepared [2014-04-20 20 ...