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

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

🔗
七00夜 2014-6-21 23:48:18 | 只看该作者
全局:
【解题思路】
用双层for循环,并引入开关变量,对两个字符数组顺次比较,一旦有相同的,将被比的字符赋值为空字符,并break内循环, 继续比较下一个字母,若内层循环遍历后也没有相同的字母出现,则return false,外层循环完全遍历后,return true
【时间复杂度】

【空间复杂度】

【gist link】
https://gist.github.com/a8e5a772f91a7159824e.git
回复

使用道具 举报

🔗
bearkino 2014-6-22 02:45:56 | 只看该作者
全局:
【解题思路】
check the string is empty or not, null or not, length is equal. Create an array to count how many times every char in the string s1 showed up using ASCII. Then, loop string s2, minus 1 for each char in s2 using ASCII too. If one string is a permutation of the other, all in the array will be 0.
【时间复杂度】
O(N)
【空间复杂度】
O(1)
【gist link】https://gist.github.com/UncleGarden/ee937ea3ffcebad6264f
回复

使用道具 举报

🔗
jaly50 2014-6-23 17:07:07 | 只看该作者
全局:
【解题思路】
  看s1是不是s2的permutation:
    先建一个asccii的数组,记录s1中每个字符出现的次数;并减去s2中每个字符出现的次数
   最后遍历这个数组,看看是否每个字符的出现次数都为零
【时间复杂度】 o(n)
【空间复杂度】o(1)
【gist link】 https://gist.github.com/jaly50/da53a06f15bdbf720cdd
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】

评分

参与人数 1大米 +1 收起 理由
ivycheung1208 + 1 不会贴里点评…gist已review……

查看全部评分

回复

使用道具 举报

🔗
ivycheung1208 2014-6-24 03:21:25 | 只看该作者
全局:
本帖最后由 ivycheung1208 于 2014-6-23 14:45 编辑

【解题思路】
compare string length, if different return false;
count character frequency, return a vector of integer;
compare character counts.
Improve: use only one vector to hold the counts, first add then substract, return false if a negative count occurs.
【时间复杂度】
O(n)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/19e3fa70461080d33475
【test case】
s1 = "", s2 = ""; return true
s1 = "", s2 = "1", return false


to楼上:不知道怎么在帖子里点评诶不过我在gist上给你review啦


回复

使用道具 举报

🔗
sanguine 2014-6-24 20:18:17 | 只看该作者
全局:
readman 发表于 2014-6-16 00:21
【解题思路】
first loop add char to int array
second minus char in int array

空间复杂度为什么是O(1)?

不是利用了额外Int[]空间吗?

一直搞不清空间复杂度如何计算==
回复

使用道具 举报

🔗
sanguine 2014-6-24 20:36:52 | 只看该作者
全局:
dreamhit 发表于 2014-6-17 06:06
Solution 1:
Sort the arrays, and compare the element one by one.
Time complexity O(nlogn)

为什么Solution1的space complexity是O(1),而Solution2是O(n),一直都不太会计算space complexity~~~

求解
回复

使用道具 举报

🔗
sanguine 2014-6-24 20:39:40 | 只看该作者
全局:
zhenzhenanan 发表于 2014-6-17 06:46
【解题思路】
1. 建立一个int数组,初始化为全0,长度为ASCII字符种类(256)。
2. 遍历第一个字符串,记 ...

1. first to check whether the length of the two input string is equal is very important! Don't forget about it!
2. After Check the length of the two strings, there is no need to traverse the array again(the 3rd for loop), because if the two string have the same characters, and the count of a Key is not equal to 0 that means the two strings have different length!
回复

使用道具 举报

🔗
sanguine 2014-6-24 20:55:18 | 只看该作者
全局:
fang_wu 发表于 2014-6-17 17:57
【解题思路】先把两个字符串转换为java 数组,然后排序,然后转换为string,用java来比较
【时间复杂度】O ...

Actually, you do not need to re-convert to string. because the equal function between two string is also a for loop statement to check each character.
But using the toString() method will create another two String type, because the string is immutable.
回复

使用道具 举报

🔗
sanguine 2014-6-24 21:01:00 | 只看该作者
全局:
larry 发表于 2014-6-18 13:14
【解题思路】Use the thought we got in CC1.1
【时间复杂度】O(N)
【空间复杂度】O(1)

no need to use the 3rd for-loop actually (more detail you can look the 47 floor)

What's more, you can do some check in the second for-loop, and it will return false before traverse all the characters in the int[]
回复

使用道具 举报

🔗
fang_wu 2014-6-24 21:15:40 | 只看该作者
全局:
sanguine 发表于 2014-6-24 20:55
Actually, you do not need to re-convert to string. because the equal function between two string i ...

为啥我试过不行啊?如果不转换的话,只是一个数组,那么只会比较首地址,在java中的equal,只有转换为string
回复

使用道具 举报

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

本版积分规则

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