Cracking the coding interview--Q2.1
原文:
Write code to remove duplicates from an unsorted linked list.
FOLLOW UP
How would you solve this problem if a temporary buffer is not allowed?
译文:
删除链表中的重复元素,
另外,如果不使用临时存储空间怎么做?
解答:
如果空间没有限制,那么用一个map来标记链表中出现过的元素,然后从头到尾扫一遍链表,把出现过的删除就行了。时间复杂度O(n)。
如果限制空间,那么可以设置两个指针n、m,当n指向某个元素时,m把该元素后面与它相同的元素删除, 时间复杂度O(n2)。
import java.util.HashMap; public class List {
int data;
List next; public List(int d) {
this.data = d;
this.next = null;
} void appendToTail(int d) {
List end = new List(d);
List n = this;
while (n.next != null) {
n = n.next;
}
n.next = end;
} void print() {
List n = this;
System.out.print("{");
while (n != null) {
if (n.next != null)
System.out.print(n.data + ", ");
else
System.out.println(n.data + "}");
n = n.next;
}
} void removeDuplicate() {
HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
List n = this;
map.put(Integer.valueOf(n.data), true);
while (n.next != null) {
if (map.containsKey(Integer.valueOf(n.next.data))) {
n.next = n.next.next;
} else {
map.put(Integer.valueOf(n.next.data), true);
n = n.next;
}
}
} void removeDuplicate1() {
List n = this;
List m;
while (n != null) {
m = n;
while (m.next != null) {
if(m.next.data == n.data)
m.next = m.next.next;
else
m = m.next;
}
n = n.next;
}
} public static void main(String args[]) {
List list = new List(0);
list.appendToTail(1);
list.appendToTail(2);
list.appendToTail(2);
list.appendToTail(3);
list.appendToTail(3);
list.appendToTail(4);
list.appendToTail(1);
list.appendToTail(2);
list.appendToTail(0);
list.print();
//list.removeDuplicate();
list.removeDuplicate1();
list.print();
}
}
Cracking the coding interview--Q2.1的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- HTTP学习笔记6-请求报头
29,请求报头: 请求报头允许客户端向服务器端传递该请求的附加信息以及客户端自身的信息. 30,Accept: Accept请求报头域用于指定客户端接受哪类型的信息,例如:Accept: image/ ...
- 001_python变量命名规范(待实践一遍)
参考: http://blog.sina.com.cn/s/blog_62f28d560100xv85.html https://my.oschina.net/leejun2005/blog/3871 ...
- 一个非常有趣的算法程序(有趣只针对程序猿)就是Josephus问题
大概花了一个晚上搭一个中午的时间,完善了一个关于Josephus的程序,这个Josephus游戏可是非常经典的算法,作为一个想从事软件的人最好能够理解一下,毕竟这个计算机教材上也讲过类似题目,具体的关 ...
- 【高级算法】模拟退火算法解决3SAT问题(C++实现)
转载请注明出处:http://blog.csdn.net/zhoubin1992/article/details/46453761 ---------------------------------- ...
- Robotium -- 使用JunitReport导出测试报告
使用Robotium进行测试的时候,要想可以导出明了的测试结果,可以使用junitreport来实现 junit-report下载地址:https://github.com/jsankey/andro ...
- Windows下搭建MySQL Master Slave[转]
Windows下搭建MySQL Master Slave 一.背景 服务器上放了很多MySQL数据库,为了安全,现在需要做Master/Slave方案,因为操作系统是Window的,所以没有办法使用k ...
- vector容器经常用法
容器简单介绍 定义及初始化 末尾插入元素 遍历 size 函数是能够动态添加的 通过下标操作添加改变vector内容不是安全的操作 仅能对已存在元素进行下标操作不存在会crash 将元素一个容器复制给 ...
- Linux磁盘设备文件(sda,sdb,sdc…)变化问题
在Linux下往往会碰到这样的问题,磁盘的设备文件,比如/dev/sda, sdb, sdc等等在某些情况下会混乱掉,比如sda变成了sdb或者sdc变成了sdb等等,这样无形中会导致磁盘设备管理的混 ...
- [转] JSON for java入门总结
一.JSON介绍 JSON(JavaScript Object Notation),类似于XML,是一种数据交换格式,比如JAVA产生了一个数据想要给JavaScript,则除了利用XML外,还可以利 ...
- [转] IPC之管道、FIFO、socketpair
管道和FIFO作为最初的UNIX IPC形式,现在已用得较少.SocketPair可作为全双工版本的管道,较为常用,这里简单做个笔记 管道 * 只用于有亲缘关系的进程间通信 * 单向,即半双工 (双向 ...