123
返回列表 发新帖
楼主: wrj5518
跳转到指定楼层
上一主题 下一主题
收起左侧

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

🔗
guchang 2014-7-5 13:24:27 | 只看该作者
全局:
【解题思路】快慢指针知道middle node,分为first和second两个half。second进行reverse,然后做比较。相等即回文
【时间复杂度】O(n)
【空间复杂度】O(1)
【gist link】https://gist.github.com/guchang/f929399108d505918262
回复

使用道具 举报

🔗
whiteflower 2014-7-6 09:43:02 | 只看该作者
全局:
【解题思路】
Use a fast pointer(iterator) and a slow pointer(iterator).
Fast pointer increments twice each step, while slow pointer
increments once, and push the value into a stack. When fast
pointer reaches end of the list, slow pointer is just in the
middle. Then move slow pointer step by step and compare with
the values stored in stack.
【时间复杂度】O(N)
【空间复杂度】O(N)
【gist link】https://gist.github.com/JoshuaTang/57c19338b7177820cb3a
回复

使用道具 举报

🔗
Tsien 2014-7-7 00:00:18 | 只看该作者
全局:
//【解题思路】
//利用递归,从中间向两边走
//【时间复杂度】
//O(n)
//【空间复杂度】
//O(1)
//【gist link】
https://gist.github.com/Tsien/2b657a0bc23f0ca63350
回复

使用道具 举报

🔗
tonygxxx1212 2014-7-7 05:57:40 | 只看该作者
全局:
【解题思路】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
回复

使用道具 举报

全局:
【解题思路】
Use stack to store the values of the first half nodes
when traversing the second half nodes, compare the value with the one on the top of the stack to check whether it's palindrome

【时间复杂度】
O(n)


【空间复杂度】
O(n)


【gist link】
https://gist.github.com/happyWinner/c144f36610798c6accc3

评分

参与人数 1大米 +2 收起 理由
kimiflasky + 2 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
sanguine 2015-1-1 07:28:48 | 只看该作者
全局:
Solution1
Idea: using two pointers, one slow and one fast, when fast met the tail, which means the slow meet the middle node, and reverse second half of the list, and contrast with the first half, if equal, which means the list is palindrome

Time Complexity: O(n)
Space Complexity: O(n)


Solution2
Idea: Instead of using extra List to store the second half the input list, we can simple use the stack to store the info.

Time Complexity: O(n)
Space Complexity: O(n)

Code:
http://www.jyuan92.com/post-477
回复

使用道具 举报

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

本版积分规则

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