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

[CareerCup] CareerCup 9.2

全局:

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

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

x
Write a method to sort an array of strings so that all the anagrams are next to each other.


上一篇:CareerCup 9.1
下一篇:McAfee Interview Question for Developer Program Engineers
🔗
modifiedname 2012-8-2 01:16:18 | 只看该作者
全局:
what are anagrams: A word, phrase, or name formed by rearranging the letters of another, such as cinema, formed from iceman.
回复

使用道具 举报

🔗
 楼主| BinaryWitch 2012-8-2 21:50:49 | 只看该作者
全局:
怎么没人做这个...
回复

使用道具 举报

🔗
parker0203 2012-8-3 04:31:48 | 只看该作者
全局:
不太明白是什么意思。。。
回复

使用道具 举报

🔗
 楼主| BinaryWitch 2012-8-3 04:34:10 | 只看该作者
全局:
parker0203 发表于 2012-8-3 04:31
不太明白是什么意思。。。

anagrams 就像2楼解释的那样
回复

使用道具 举报

🔗
parker0203 2012-8-3 04:42:05 | 只看该作者
全局:
BinaryWitch 发表于 2012-8-3 04:34
anagrams 就像2楼解释的那样

就是先sort 每个string 然后再吧这些strings sort一下?
回复

使用道具 举报

🔗
 楼主| BinaryWitch 2012-8-3 05:25:39 | 只看该作者
全局:
parker0203 发表于 2012-8-3 04:42
就是先sort 每个string 然后再吧这些strings sort一下?

差不多 只是把原 string 改变了 不过也好解决

可以给每个 string 加入一个 sort 之后的 string 当作关键字排 费空间
也可以写新的 comparator 每次复制整个 string 比较 sort 的结果 费时间




回复

使用道具 举报

🔗
parker0203 2012-8-5 04:49:26 | 只看该作者
回复

使用道具 举报

🔗
parker0203 2012-8-5 04:51:50 | 只看该作者
全局:
parker0203 发表于 2012-8-5 04:49
https://github.com/parker0203/careercup/blob/master/9_2.cpp

swap string 用各种指针引用都报错, 最后还是用库函数了, 注释里的swap请忽略
回复

使用道具 举报

🔗
writecoffee1 2012-8-6 01:28:32 | 只看该作者
全局:
https://github.com/writecoffee/c ... blob/master/9.2.cpp


1. Solution 1: Sacrifice the Space
• For each string, record the appearence of the 26 letters and make pair. Do a quick sort.
• The record work is proportional to the size of each strings, namely O(m). And the compar-
ison for the record sets take constant time (thinking the maximum size of these 26 letters).
At last, the quick sort which takes O(n log n) timing dominates the total timing complexity.
• As for the space complexity, extra mapping set for each string consumes O(n).
2. Solution 2: Sacrifice the Timing
• Sort the string list in place, where we need to copy the strings, compare the anagrams every
time comparing each list item.
• For the sake of each comparison inside the outer quick sort loop, the total time complexity
is O(mn log n).
• Because we exploit temporary space for each comparison, the space complexity could be
considered O(1).



========PS===========
1. hard to modify a quicksort to a version which can sort with equivalent values.
2. overloading the comparator in C++ is a bit tricky for me.
回复

使用道具 举报

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

本版积分规则

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