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

[CareerCup] [第二轮] 2/18-2/24 CareerCup 1.1

🔗
bainikolaus 2013-2-17 16:44:25 | 只看该作者
全局:
先排序, 然后再把每个字符和它后面一个字符比较:
https://gist.github.com/bainikolaus/4970686
回复

使用道具 举报

🔗
bainikolaus 2013-2-17 16:50:09 | 只看该作者
全局:
Arthur2012 发表于 2013-2-17 15:19
Algorithm_1: There are two cycles and everytime pick one element of string to compare with the rest ...

代码很清晰写的不错。一点小建议是在for loop里面用for(int i = 0, strLen = str.len(); i < strLen; i++) 来代替 for(int i = 0, i< str.len(); i++)。
回复

使用道具 举报

🔗
Arthur2012 2013-2-17 17:23:39 | 只看该作者
全局:
恩恩,我明白了,每次都算了次str.len(),太感谢了,我现在就改
回复

使用道具 举报

🔗
yang_cs 2013-2-17 17:31:37 | 只看该作者
全局:
bainikolaus 发表于 2013-2-17 16:50
代码很清晰写的不错。一点小建议是在for loop里面用for(int i = 0, strLen = str.len(); i < strLen; i++ ...

嗯,我一直都这个写法,嘿嘿,优化常数复杂度
回复

使用道具 举报

🔗
lvchaoshuai 2013-2-17 17:36:31 | 只看该作者
全局:
bainikolaus 发表于 2013-2-17 16:50
代码很清晰写的不错。一点小建议是在for loop里面用for(int i = 0, strLen = str.len(); i < strLen; i++ ...

其实C++里string的length函数是返回常量吧,并不是类似strlen来通过计算获得长度的。返回的应该是个内部常量。所以按原来的写法反而不用额外变量(增加一点点访问函数的开销可以不计)。不过原来的写法的话.length()返回的size_t类型在32位机下是unsigned int,跟int直接比不太好。
最好写成 for (int i = 0; i < (int)str.len(); i++),简洁清晰。
回复

使用道具 举报

🔗
lvchaoshuai 2013-2-17 17:39:26 | 只看该作者
全局:
Arthur2012 发表于 2013-2-17 17:23
恩恩,我明白了,每次都算了次str.len(),太感谢了,我现在就改

可参考:http://stackoverflow.com/questio ... or-the-string-class
回复

使用道具 举报

🔗
grassgigi 2013-2-17 17:43:06 | 只看该作者
全局:
if we can use additional array, then use bitmap for all characters
with no additional data structures, use sorting + comparing adjacent element

https://gist.github.com/chrislukkk/4970718
回复

使用道具 举报

🔗
Arthur2012 2013-2-17 17:50:08 | 只看该作者
全局:
lvchaoshuai 发表于 2013-2-17 17:39
可参考:http://stackoverflow.com/questions/5615239/c-strlen-for-the-string-class

thx, again
回复

使用道具 举报

🔗
jimwallet 2013-2-17 20:08:07 | 只看该作者
全局:
sort array first, and compare the adjacent elements. O(nlgn).

https://gist.github.com/njcongtou/4971237
回复

使用道具 举报

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

本版积分规则

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