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

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

🔗
weep_willow 2013-2-28 06:09:25 | 只看该作者
全局:
本帖最后由 weep_willow 于 2013-2-28 06:14 编辑

use two pointers front and back, the distance betwen these two pointers is K. so when the front pointer reaches the end, the back pointer is exactly at the location of kth to the last element.
Time complexity: O(n)
c/c++ 实现。初来咋到,望多多指教,谢谢大家,嘿嘿。
https://github.com/StanleyLee/CareerCup/blob/master/2_2.cpp






回复

使用道具 举报

🔗
莫有逆臣 2013-2-28 07:01:35 | 只看该作者
全局:
最开始连题都没理解。。。才知道kth to last是倒数第k个的意思。。。照着1楼的思路写的。。纯练手
java
https://gist.github.com/krisliu/5052641
回复

使用道具 举报

全局:
Create two 'pointers', first one and second one have k nodes in between. Then go over the list, until the second one reaches the end. Same ideas as most of above.

https://gist.github.com/lynntian/5053658
回复

使用道具 举报

🔗
fuxiang90 2013-2-28 10:46:39 | 只看该作者
全局:
jimwallet 发表于 2013-2-24 22:02
https://gist.github.com/njcongtou/4999347

1。 use two pointers, k interval between them.

you should juge n ,if n <= 0  
回复

使用道具 举报

🔗
fuxiang90 2013-2-28 10:47:27 | 只看该作者
全局:
mapleman 发表于 2013-2-25 02:10
这道题二楼所说的那种做法,先遍历一遍拿到长度再去找倒数第k个,比答案差在哪里呢?
谢谢!

如果是n + n  的复杂度
回复

使用道具 举报

🔗
weixc1234 2013-2-28 19:56:37 | 只看该作者
全局:
Java language.
1.简单直接的办法,先遍历求长度,再求倒数k个元素
2.两个指针,距离为k,这还是参考了前面的同学,果然基础还是不行
3.其实我觉得将链表逆序然后反向求也是可以的,不过时间复杂度也没有优化,只能算一种思路吧。

https://gist.github.com/Sayary/5056257
回复

使用道具 举报

🔗
qeroqero 2013-3-1 00:03:47 | 只看该作者
全局:
find the kth to last element"equals" find N(Linked list lenth)-kth+1 to the first element. One pointer count the N, the other pointer point to N-K+1
回复

使用道具 举报

🔗
leonsu777 2013-3-1 03:09:20 | 只看该作者
回复

使用道具 举报

🔗
leonsu777 2013-3-1 03:10:45 | 只看该作者
全局:
本帖最后由 leonsu777 于 2013-3-1 03:13 编辑

@Fanyare recursive的办法 优雅在于利用了function stack
回复

使用道具 举报

🔗
cjsp 2013-3-2 15:03:15 | 只看该作者
全局:
https://gist.github.com/flatlight/5069975
用两个指针和遍历算长度再遍历不一样的吗...
回复

使用道具 举报

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

本版积分规则

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