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

[CareerCup] 【第三轮】6.30-7.6 CareerCup 2.7

全局:

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

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

x
2.7  Implement a function to check if a linked list is a palindrome.


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


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





上一篇:學過java的幫忙看看吧,head first java里的小问题
下一篇:【第三轮】6.30-7.6 CareerCup 3.1
推荐
Neal 2014-7-1 07:37:48 | 只看该作者
全局:
【解题思路】reverse the second half, compare it with the first half, then restore the list.
【时间复杂度】O(N)
【空间复杂度】O(1)
【gist link】https://gist.github.com/nealhu/a8fb05f7c9db8b38b403

评分

参与人数 1大米 +3 收起 理由
MCwong + 3 有启发,我reverse了整个,比较前一半,真.

查看全部评分

回复

使用道具 举报

全局:
【解题思路】c++ 写了两个:第一种用了数组,逆序存入,在重新iterate对比,完全一致则true,遇到不一致返回false
                                        第二种是看了书启发的,快慢runne+stack, push 一半, 之后慢指针和pop出来的对比,遇到不一致返回false, 否则true
                                        (需要注意的是,list 长度可为odd/even, 写判断条件要考虑周全)
【时间复杂度】O(n) 方法一要iterate两遍,方法二一遍
【空间复杂度】O(n) 数组要得相对多一点,stack少一半
https://gist.github.com/xun-gong/6fae52f025d0a6aab959
回复

使用道具 举报

🔗
grassgigi 2014-6-29 22:55:19 | 只看该作者
全局:
【解题思路】
Recursion

【时间复杂度】
O(N) call depth

【空间复杂度】
O(1)

【gist link】
https://gist.github.com/chrislukkk/1a1a5cc9fafe13b0d87e

补充内容 (2014-7-1 12:46):
【空间复杂度】
O(N) - recursion depth
回复

使用道具 举报

🔗
林微熙 2014-6-30 08:28:36 | 只看该作者
全局:
本帖最后由 林微熙 于 2014-6-29 17:29 编辑

【gist link】【解题思路】
Recursion

【时间复杂度】
O(N)

【空间复杂度】
O(1)

【gist link】https://gist.github.com/hilda8519/55ba79b63e25ea971166
回复

使用道具 举报

🔗
heycinderella 2014-6-30 08:46:46 | 只看该作者
全局:
本帖最后由 heycinderella 于 2014-6-30 08:48 编辑

【解题思路】
其实如果让用extra data structure就好多方法不过都比较trivial,不然就用recursion,@serolins 或者像书上那样。另外如果input是null是返回true还是false应该是问interviewer。书上的意思好像是true

【时间复杂度】
O(N)

【空间复杂度】
O(N) because of recursion

【gist link】
https://gist.github.com/XiaoxiaoLi/97a862556e61369b59d1

点评

用了好多不同的算法实现 点赞!!  发表于 2014-6-30 09:59
回复

使用道具 举报

🔗
ivycheung1208 2014-6-30 09:56:39 | 只看该作者
全局:
本帖最后由 ivycheung1208 于 2014-6-29 21:01 编辑

【解题思路】Assume data tye is char
1. reverse the list and compare each element.
2. push first half of the list into a stack, continue traverse the list and pop out if the second half matches the first half. return true if the stack is empty after traversal.
3. recursion...
【时间复杂度】
O(2N)
O(N)
【空间复杂度】
O(N)
O(N/2)
【gist link】
https://gist.github.com/ff5d9972e02df9306886
【test case】
null list returns true
回复

使用道具 举报

🔗
bitcpf 2014-7-1 00:30:21 | 只看该作者
全局:
【解题思路】Build a new list reverse the original list, then compare their elements
【时间复杂度】O(N)
【空间复杂度】O(N)
【gist link】
https://gist.github.com/bitcpf/3ed5522deab119c1f576
回复

使用道具 举报

🔗
donnice 2014-7-1 03:11:45 | 只看该作者
全局:
【解题思路】把linkedlist用list.toArray转化为list后进行比较
【时间复杂度】O(N)
【空间复杂度】O(N)
【gist link】
https://github.com/donnice/donnice/blob/master/Q2.7
回复

使用道具 举报

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

本版积分规则

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