楼主: Fanyare
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] [第二轮] 2/25-3/3 CareerCup 2.2

🔗
ThunderXu 2013-2-25 21:26:49 | 只看该作者
全局:
https://gist.github.com/ThunderXu/5029766
1. make two pointer: former and later, former points to the head and later points to the kth element;
2. move two pointers to the end, when later arrives the end, start outputing elements that former points to until it also arrives the end.
回复

使用道具 举报

🔗
 楼主| Fanyare 2013-2-26 03:59:33 | 只看该作者
全局:
https://github.com/quantumrose/C ... /2.2.KthToLast.java

method1: first traverse the linked list to get its length, then find the kth to last element. O(n) time, O(1) space
method2: use two pointers k nodes apart and move them together. O(n) time, O(1) space
method3: recursive. O(n) time, O(n) space
不太明白这题用recursive的优势在哪里......
回复

使用道具 举报

🔗
ryancooper 2013-2-26 09:36:38 | 只看该作者
全局:
zuohr 发表于 2013-2-24 15:36
Use two "pointers" iterate through the linked list, the second one lags behind the first one by k po ...

Brilliant idea!
回复

使用道具 举报

🔗
ryancooper 2013-2-26 09:39:50 | 只看该作者
全局:
grassgigi 发表于 2013-2-25 01:18
刚开始看成kth largest element to the end..
kth element的话,一样是两个pointer,tracking down to the ...

Me too! if it's indeed to find the kth largest element, then I think we have to read all the values in to memory first. This is the only method I know to solve this alternate problem, maybe you and other smart guy can figure out a much more efficient way
回复

使用道具 举报

🔗
moophis 2013-2-26 19:37:39 | 只看该作者
全局:
回复

使用道具 举报

🔗
Kimurate 2013-2-26 19:46:46 | 只看该作者
全局:
本帖最后由 Kimurate 于 2013-2-26 19:51 编辑
starcroce 发表于 2013-2-25 04:04
时间复杂度都是O(n),但是改进后的做法只需要遍历一遍,我的最初想法相当于遍历两遍,所以改进后常数项上 ...
这道题二楼所说的那种做法,先遍历一遍拿到长度再去找倒数第k个,比答案差在哪里呢?

直观的方法是一个指针遍历两遍:第一次指针遍历整个list(距离n),再返回原处,第二次遍历n-k的距离,加起来遍历的距离是2n-k
1楼的做法是用两个指针遍历一遍:一个指针遍历整个list(距离n),另一个指针遍历n-k的距离,加起来还是2n-k
我的理解是:在 k 比较小,而链表很大的时候,第二种方法能利用空间局部性,可以快一点,因此1楼的解法会比较好。
回复

使用道具 举报

🔗
Kimurate 2013-2-26 19:56:38 | 只看该作者
全局:
Inspired by zuohr.
* Method: Use two pointers to iterate through the list,
* the first one has k distance to the second one, when the
* second one gets to the end, return the first pointer.

C language:https://gist.github.com/hukun01/5037818
回复

使用道具 举报

🔗
champoier 2013-2-27 05:48:22 | 只看该作者
全局:
2个指针,间隔k,直到后面的指针扫到尾。
https://gist.github.com/skxie/6c18c92944d7ee609f95
回复

使用道具 举报

🔗
sing1ee 2013-2-27 15:40:20 | 只看该作者
全局:
gist: https://gist.github.com/sing1ee/5046004

use two pointer method when the first pointer step forward k steps, the
second pointer start to move. when the first pointer goes to the end, the
second points to the last kth element.
回复

使用道具 举报

🔗
lazyGoose 2013-2-27 23:23:34 | 只看该作者
全局:
Move the head point to "start head point"

https://gist.github.com/lazyGoose/5048684
回复

使用道具 举报

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

本版积分规则

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