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

[CareerCup] 【第三轮】6.16-6.22 CareerCup 1.3

🔗
兰橘清檬 2014-6-17 02:03:57 | 只看该作者
全局:
【解题思路】
若两个字符串长度不同,则返回 false
设字符集为 ascii(256个),利用一个大小为256的 int 数组;
第一遍对 s 中所有字符遍历,将每个字符出现的次数计入数组;
第二遍对 t 中所有字符遍历,在数组中减去字符出现的次数,若减去后小于 0,则返回 false;
返回 true
【时间复杂度】
n
【空间复杂度】
1
【gist link】
https://gist.github.com/JoyceeLee/826c8c3ea2bdedfe9622
回复

使用道具 举报

🔗
dreamhit 2014-6-17 06:06:35 | 只看该作者
全局:
Solution 1:
Sort the arrays, and compare the element one by one.
Time complexity O(nlogn)
Space complexity O(1)

Solution 2:
Time complexity: O(n), Space complexity: O(n)

Code link:
https://gist.github.com/Jocelyn9/d288fd5e38e7313ebb65
回复

使用道具 举报

🔗
zhenzhenanan 2014-6-17 06:46:20 | 只看该作者
全局:
【解题思路】
1. 建立一个int数组,初始化为全0,长度为ASCII字符种类(256)。
2. 遍历第一个字符串,记录每一个字符出现的次数。
3. 遍历第二个字符串,每遇到一个字符,就在数组的相应元素中减去一。
4. 遍历数组,如果每一个元素都是0,则返回true;否则返回false。
【时间复杂度】
O(m+n),其中m和n分别是两个字符串的长度。
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/seemuch/af64b140e576b6ea73ad#file-1_3-cc
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】
回复

使用道具 举报

🔗
jyh橘子 2014-6-17 09:40:38 | 只看该作者
全局:
Assumptions : case sensitive     do not ignore white space
1.
【解题思路】
use a hashmap to count the frequency of  every chars in str1,   traverse str1, corresponding frequency - 1,  return false when the frequency is going to be negative
【时间复杂度】
O(n)
【空间复杂度】
O(1)
2.
【解题思路】
compare two  sorted char arrays   element by element  or  compare two strings generated from sorted char arrays
【时间复杂度】
O(nlogn)
【空间复杂度】
O(1)
【gist link】https://gist.github.com/jyhjuzi/6a48769758a67ee2a7ae
回复

使用道具 举报

🔗
chouclee 2014-6-17 15:29:13 | 只看该作者
全局:
本帖最后由 chouclee 于 2014-6-17 15:44 编辑

【解题思路】用两个HashMap分别统计字符串里字符出现的频率,然后比较两个HashMap,频数不一致的话return false。对大小写敏感,对空格敏感。如果想对大小写不敏感,在统计前可以先将字符串全部toLowerCase()或者toUpperCase(),对空格不敏感,可以在统计完成后,判断频数时跳过blank character
【时间复杂度】O(n)
【空间复杂度】O(1) (如果不可以破坏原string,调用toLowerCase()或toUpperCase()就是O(n))
【gist link】https://gist.github.com/chouclee/0fb6ccc84d604d50a14e
【test case】a = "我为人人"; b = "人人为我";

回复

使用道具 举报

🔗
fang_wu 2014-6-17 17:57:11 | 只看该作者
全局:
【解题思路】先把两个字符串转换为java 数组,然后排序,然后转换为string,用java来比较
【时间复杂度】O(nlogn)
【空间复杂度】O(1)
【gist link】https://gist.github.com/qiangusc/d468e3ee938dd8f2a5e2
回复

使用道具 举报

🔗
asdw3276 2014-6-18 01:46:48 | 只看该作者
全局:
【解题思路】遍历第一个字符串,根据每一个字符的ascii码令int数组中相应的元素+1.遍历第二个字符串,根据每一个字符的ascii码令int数组中相应的元素-1. 最后遍历int数组是否全部为0即可
【时间复杂度】O(n)
【空间复杂度】O(1)
【gist link】https://gist.github.com/asdw3276/f9e259ebf504db026e7d

感谢所有给我code review的小伙伴

点评

no need to use the 3rd for-loop, look the 47 floor for more detail~  发表于 2014-6-24 20:56
回复

使用道具 举报

🔗
林微熙 2014-6-18 02:03:58 | 只看该作者
全局:
【解题思路】length s!=length t,false; s,t are empty, false; put s,t from array to string ,compare they are equal or not
【时间复杂度】O(nlogn)
【空间复杂度】O(1)
【gist link】https://gist.github.com/hilda8519/3fbd305943dab4addd62

点评

48 floor for more detail  发表于 2014-6-24 20:57
回复

使用道具 举报

🔗
RealityPC 2014-6-18 08:33:57 | 只看该作者
全局:
【解题思路】loop over all chars in string1 and add one to the corresponding ascii array. do the same thing but minus one to string2. at the end, if all the ascii number have value 0, return true.
【时间复杂度】O(N)
【空间复杂度】O(1)
【gist link】https://gist.github.com/pchong90/c2badf9e895b8fbf7ee4
回复

使用道具 举报

🔗
rxTByjroA2h3 2014-6-18 13:14:44 | 只看该作者
全局:
【解题思路】Use the thought we got in CC1.1
【时间复杂度】O(N)
【空间复杂度】O(1)
【gist link】https://gist.github.com/larry-liu/c3ec4c9f7f5586864963

点评

楼下code review:我的做法和你基本一致~  发表于 2014-6-23 17:08
回复

使用道具 举报

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

本版积分规则

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