Cracking coding interview(2.1)去除LinkedList中的重复元素
来源:互联网 发布:java二级考试题库下载 编辑:程序博客网 时间:2024/06/05 03:11
2.1 Write code to remove duplicates from an unsorted linked list.
FOLLOW UP
1.brute-force time complexity: O(n^2) space complxity:O(1), 输出元素保持原有顺序
FOLLOW UP
How would you solve this problem if a temporary buffer is not allowed?
import java.util.LinkedList;import java.util.Iterator;import java.util.Collections;import java.util.Hashtable;public class Solution{//brute-force time complexity:O(n^2) space complexity:O(1)public static void removeDuplicate1(LinkedList<Integer> list){for(int i=0;i < list.size()-1;i++)for(int j=i+1;j < list.size();)if(list.get(i) == list.get(j))list.remove(j);elsej++;}//could't keep order time complexity:O(nlogn) space complexity:O(1)public static void removeDuplicate2(LinkedList<Integer> list){//sortCollections.sort(list);if(list.size() >= 2){for(int i=0;i < list.size()-1;){if(list.get(i) == list.get(i+1))list.remove(i+1);elsei++;}}}//1.keep order 2.time complexity:O(n) 3.space complexity:O(n): (worst case)public static void removeDuplicate3(LinkedList<Integer> list){Hashtable<Integer, String> hash = new Hashtable<Integer, String>();//lookup hashtable to delete repeat elementsfor(int i=0;i < list.size();){if(hash.containsKey(list.get(i)))list.remove(i);else{hash.put(list.get(i), "");i++;}}}private static void printLinkedList(LinkedList<Integer> list){Iterator it = list.iterator();while(it.hasNext()){System.out.print((Integer)it.next()+" ");}System.out.println();}public static void main(String[] args){LinkedList<Integer> list = new LinkedList<Integer>();list.add(6);list.add(2);list.add(2);list.add(3);list.add(1);list.add(4);list.add(2);list.add(3);list.add(7);list.add(2);list.add(2);list.add(10);Solution.printLinkedList(list);Solution.removeDuplicate3(list);Solution.printLinkedList(list);}}
1.brute-force time complexity: O(n^2) space complxity:O(1), 输出元素保持原有顺序
2.sort:time complexity:O(nlogn) space complexity:O(1), 输出元素为排序后结果
3.hashtable:time complexity:O(n) space complexity:O(n), 输出元素保持原有顺序
类似问题:http://blog.csdn.net/u011559205/article/details/38125405
0 0
- Cracking coding interview(2.1)去除LinkedList中的重复元素
- Cracking coding interview(2.3)灵活删除LinkedList中间元素
- Cracking the Coding Interview
- 《Cracking the coding interview》
- Cracking The Coding Interview
- Cracking the coding interview
- Cracking the coding interview
- Cracking coding interview(1.3)去掉字符串中重复字符
- 二维数组中的查找 Cracking the coding interview 9.6
- Cracking the coding interview--Q1
- Cracking the coding interview--Q17
- Cracking the coding interview--Q2
- Cracking the coding interview--Q3
- Cracking the coding interview--Q4
- Cracking the coding interview--Q5
- Cracking the coding interview--Q8
- Cracking the coding interview--Q9
- Cracking the coding interview 题目
- 最大子序列和问题
- URAL 1800 Murphy's Law(物理判断题)
- atitit.提升软件开发的效率and 质量的那些强大概念and方法总结
- HDU 题目1087 Super Jumping! Jumping! Jumping!
- hdu 1253 胜利大逃亡(bfs)
- Cracking coding interview(2.1)去除LinkedList中的重复元素
- SDUTOJ 1480 数据结构实验:哈希表——链地址法处理冲突
- 非常读一
- POJ2182 Lost Cows
- 非常读一
- 【FFT】大整数乘法
- 想入行的朋友看看这篇文章就大概知道游戏行业了
- URAL 1804 The Machinegunners in a Playoff(模拟比赛得分)
- 随机梯度下降法