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

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

全局:

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

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

x
本帖最后由 Fanyare 于 2013-2-17 10:32 编辑

Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?

发帖规范:
http://www.1point3acres.com/bbs/thread-48094-1-1.html
http://www.1point3acres.com/bbs/thread-32423-1-1.html

评分

参与人数 1大米 +10 收起 理由
Kimurate + 10 辛苦了 :P

查看全部评分


上一篇:GOOGLE 面試準備 筆記
下一篇:[第二轮] 2/18-2/24 CareerCup 1.2
🔗
edussx 2013-2-17 14:19:48 | 只看该作者
全局:
overview:construct a nested loop, compare ith char to i+1, i+2,...,nth char in that string.
if any of comparision matches, return false. return true when loops end.

https://gist.github.com/edussx/4970428

点评

C++里字符串是可以用数组的形式直接使用的,我用的JAVA似乎只能用charAt()方法,或者转化为字符数组,不过后者增加了额外的数据结构。 做法是一样的,应该还有更简单的方法。  发表于 2013-2-17 15:00
回复

使用道具 举报

🔗
weixc1234 2013-2-17 14:57:56 | 只看该作者
全局:
https://gist.github.com/Sayary/4970488

In Java language.
Build a nested loop to to traversal the string and use charAt() to get the byte of each index.
Time complexity is O(n*n).
回复

使用道具 举报

🔗
lvchaoshuai 2013-2-17 14:59:24 | 只看该作者
全局:
edussx 发表于 2013-2-17 14:19
overview:construct a nested loop, compare ith char to i+1, i+2,...,nth char in that string.
if any ...

这个是O(n^2)的,令N=max(n,charsetnum),这个题目最好的复杂度应该是O(N)的,当然O(NlogN)的方法也很好想。就是不知道额外的数组算不算additional data structures了。期待更精彩的解答哦~
回复

使用道具 举报

🔗
edussx 2013-2-17 15:10:50 | 只看该作者
全局:
lvchaoshuai 发表于 2013-2-17 14:59
这个是O(n^2)的,令N=max(n,charsetnum),这个题目最好的复杂度应该是O(N)的,当然O(NlogN)的方法也很好想 ...

复杂度还没学到,所以没想过这方面的问题

额外的数组如果算additional data structures的话,在c++里可以使用at() member function,即s.at(i)来代替s[i]

回复

使用道具 举报

🔗
ryancooper 2013-2-17 15:13:02 | 只看该作者
全局:
本帖最后由 ryancooper 于 2013-2-17 15:14 编辑
edussx 发表于 2013-2-17 14:19
overview:construct a nested loop, compare ith char to i+1, i+2,...,nth char in that string.
if any ...

Wow, so quick! If we can not use additional data structures, then it seems we have to do the brute force search(or maybe there exists a better solution, unfortunately I'm not clever enough to figure it out).

But if it's not, and if all the characters in the string fall into a alphabet, we can create a bitmap. Even in the extreme situation, we have unicode as our alphabet, which contains slightly more than 1M characters, we will only consume very little memory, like 122KB. And the algorithm runs on it only takes O(n) steps.
回复

使用道具 举报

🔗
lvchaoshuai 2013-2-17 15:18:52 | 只看该作者
全局:
edussx 发表于 2013-2-17 15:10
复杂度还没学到,所以没想过这方面的问题

额外的数组如果算additional data structures的话, ...

不是,暂时还没想到不用额外数组的O(n)方法,不知道是否存在=.=
回复

使用道具 举报

🔗
Arthur2012 2013-2-17 15:19:17 | 只看该作者
全局:
Algorithm_1: There are two cycles and everytime pick one element of string to compare with the rest chacteristics of the string.
Algorithm_2: First of all, sort the string; then compare every element with any elements before or behind it
试一试。
https://github.com/Tedatworking/ ... ster/20130217_1.cpp
回复

使用道具 举报

🔗
Arthur2012 2013-2-17 15:24:50 | 只看该作者
全局:
edussx 发表于 2013-2-17 14:19
overview:construct a nested loop, compare ith char to i+1, i+2,...,nth char in that string.
if any ...

恩恩,正解,不知道有没有在不增加数据结构的情况下的O(n)算法
回复

使用道具 举报

🔗
cqx83 2013-2-17 16:36:31 | 只看该作者
回复

使用道具 举报

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

本版积分规则

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