查看: 5175| 回复: 36
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】6.23-6.29 CareerCup 2.1

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 wrj5518 于 2014-6-23 09:32 编辑

2.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?

回复解法可以按照以下格式来
【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】
---------------Optional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】


Notice:
1、记得在程序注释中表明自己算法的时间、空间复杂度
2、代码难懂之处加注释
3、每道题目有对应的帖子,除了贴解法,欢迎讨论,集思广益
4、任何未尽之处,欢迎回报名帖提问,我会进一步作出修改。

评分

参与人数 1大米 +10 收起 理由
林微熙 + 10 坚持的不错,再接再厉!

查看全部评分


上一篇:【第三轮】6.23-6.29 CareerCup 1.8
下一篇:【第三轮】6.23-6.29 CareerCup 2.2
推荐
grassgigi 2014-6-23 03:05:38 | 只看该作者
全局:
【解题思路】
Use hash table to record elements showed up earlier, removed node if it shows up again later.
Follow up: for each node, scan through all of its following nodes and remove duplicates;

【时间复杂度】
O(N)
Follow up: O(N^2)

【空间复杂度】
O(N)
Follow up: O(1)

【gist link】
https://gist.github.com/chrislukkk/fc2ce325c06ab6e47c1e
回复

使用道具 举报

全局:
【解题思路】
1. 用hash set:建立一个hash set,并遍历链表。如果链表的点在hash set里面,则删除这个点;如果不在的话,插入到hash set里。
2. 不用hash set:两个指针,第一个current就正常遍历链表;第二个runner每一次都遍历current之前的,并查找重复的。如果有,则current所指向的。
3. 需要注意的是,删除current的时候并不能直接删除,因为需要让current之前的那一个的next指向current之后的那一个。所以我们让current永远指向当前node的前一个即可。
【时间复杂度】
用hash set: O(n)
不用hash set: O(n^2)
【空间复杂度】
用hash set: O(n)? (其实一直不知道时候hash table / hash set的时候,空间复杂度怎么算,求指点)
不用hash set: O(1)
【gist link】
https://gist.github.com/seemuch/af64b140e576b6ea73ad#file-2_1-cc
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
回复

使用道具 举报

推荐
chouclee 2014-6-23 11:15:26 | 只看该作者
全局:
【解题思路】HashSet,遇到不重复的,添加进LinkedList,最后返回LinkedList。不允许用buffer的话,用brute force求解。
也考虑过先用quicksort,但是single linkedlist的交换不能做到array里面的O(1),并且two-way quicksort在应付[a,a,a,a,a,a]这种重复元素很多的数组时,复杂度接近于O(n^2)。3-way quicksort可以解决这个问题,但是算法里需要头尾两个指针依次向中间移动,而single linkedlist不能回溯到前一个元素。
【时间复杂度】O(n) / O(n^2)
【空间复杂度】O(n) / O(1)
【gist link】
HashSet: https://gist.github.com/chouclee/eba793a770b25674ad78
Brute-force: https://gist.github.com/chouclee/fddce83f4860442aa333
回复

使用道具 举报

🔗
qianhuang 2014-6-23 09:58:38 | 只看该作者
全局:
【解题思路】
1. If the data of each node comes from a fixed character set like ASCII, we can create a array to store whether the character appear in the string. Thus, runtime is O(n), space is O(size of character set). If we don't know the range of data, we can use hashtable.
2. If the space must be O(1), we can search each character in the string, runtime time is O(n^2).
【时间复杂度】
1. O(n)
2. O(n^2)
【空间复杂度】
1. O(n)
2. O(1)
【gist link】
https://gist.github.com/qianhuang/fa817dd6f55c5c21aa39
回复

使用道具 举报

🔗
readman 2014-6-23 12:21:58 | 只看该作者
全局:
【解题思路】
three pointers
【时间复杂度】
n2
【空间复杂度】
1
【gist link】
https://gist.github.com/gaoyike/badaff09f773e2a7707e


弱爆了我
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
回复

使用道具 举报

🔗
habina 2014-6-23 14:04:36 | 只看该作者
全局:
Solution              : I am gonna sort the linked list first, and check value one by one, and reconnect the link and free the duplicate node
Time Complexity  : O(n^2)
Space Complexity : O(n)
Gist Link              : https://gist.github.com/habina/3896531754d629b2032f
回复

使用道具 举报

🔗
bearkino 2014-6-23 14:35:52 | 只看该作者
全局:
【解题思路】
第一步很简单想到用各种hash来去掉重复值。
follow up的就是在链表中一个一个对照的去掉重复值。
P.S. Test Case各种写不好。。直接参考一楼了  0 0

【时间复杂度】
O(N)
Follow up: O(N^2)

【空间复杂度】
O(N)
Follow up: O(1)

【gist link】
https://gist.github.com/UncleGarden/55470d66c3823ce9144e
回复

使用道具 举报

🔗
fang_wu 2014-6-23 19:20:59 | 只看该作者
全局:
【解题思路】
Hashmap
直接o(n2)的复杂度,循环比较

【时间复杂度】
O(N)
Follow up: O(N^2)

【空间复杂度】
O(N)
O(1)

【gist link】
https://gist.github.com/qiangusc/38a45fa1b39a0185b31a
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表