avatar
EB1A交了140两周了# Immigration - 落地生根
s*n
1
面试题目:
有2个字符串,消除第一个字符串中第二个字符串包含的所有字母。
例如:
string1: helloworld
string2: abcdef
output: hlloworld
面试的时候我说了很笨的办法。因为不许allocate新的空间。
大家帮我说说最好的解法应该是什么?谢谢了
avatar
l*g
2
EB1A交了140两周了,TSC,还是internal review。因为背景类似的朋友都是在不到一
个月内approve的,LG急着想要知道结果,有想要烧钱pp的冲动。请问目前EB1A是不是
都慢下来了呢?因为看到有说去年六月份交140的还在等。还是说等的时间越长说明情
况越不妙呢?
avatar
r*y
3
sort string2 first? then binary search ?

【在 s*******n 的大作中提到】
: 面试题目:
: 有2个字符串,消除第一个字符串中第二个字符串包含的所有字母。
: 例如:
: string1: helloworld
: string2: abcdef
: output: hlloworld
: 面试的时候我说了很笨的办法。因为不许allocate新的空间。
: 大家帮我说说最好的解法应该是什么?谢谢了

avatar
R*n
4
着急要结果,就pp。不pp,准备等7-8个月
avatar
g*s
5
after deleting 26 chars, use these space as a simple hashmap.

【在 r*******y 的大作中提到】
: sort string2 first? then binary search ?
avatar
f*r
6
好长啊

【在 R******n 的大作中提到】
: 着急要结果,就pp。不pp,准备等7-8个月
avatar
r*y
7
smart idea

【在 g***s 的大作中提到】
: after deleting 26 chars, use these space as a simple hashmap.
avatar
m*t
8
没pp平均4个月

【在 l*******g 的大作中提到】
: EB1A交了140两周了,TSC,还是internal review。因为背景类似的朋友都是在不到一
: 个月内approve的,LG急着想要知道结果,有想要烧钱pp的冲动。请问目前EB1A是不是
: 都慢下来了呢?因为看到有说去年六月份交140的还在等。还是说等的时间越长说明情
: 况越不妙呢?

avatar
c*t
9
So how do we efficiently delete the target letters without additional
storage?
avatar
h*n
10
果然要always think of hash first...

【在 g***s 的大作中提到】
: after deleting 26 chars, use these space as a simple hashmap.
avatar
w*s
11
why deleting 26 chars??? Not really understanding...

【在 g***s 的大作中提到】
: after deleting 26 chars, use these space as a simple hashmap.
avatar
g*y
12
delete 26个char是什么意思?同不明白。
avatar
d*l
13
我感觉这个问题可以用grass之前在另一个thread里提到的那个方法,就是
while(s[i] != s[s[i]-'a']) swap(s[i], s[s[i]]);
这里写的比较粗略,没考虑大写字母。相当于把每个字母换到对应的位置上,然后可以
作为hashmap来用。不过这需要string2长度至少是26,而且会破坏string2,不知道是
否符合要求。然后扫一遍string1就可以了。
avatar
i*e
14
我想就是利用两个前后指针扫描,然后首先可以用简单的 linear search 来寻找重复
的。直到后指针与前指针的距离为 26,那也意为着有 26 个连续的空位。这时候就把
后指针指向和之后的所有元素往前移 26位,然后利用最后的 26 空位来当 hash。
也就是 grass 所说的是从 string1 里删除了二十六个重复的字母之后,利用剩下的二十六
个空位来当作hash来用。
这是考虑到 string1 的长度非常长。但是有一个最坏情况就是万一很久都没碰上重复的字母怎么办?抑或是 string1 产生重复的个别字母不超过 string2 的长度?那这基本上就跟 worst case 一样,26*n 的比较次数,那还不如把 string2 排好序,然后做 binary search。
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

【在 s*******n 的大作中提到】
: 面试题目:
: 有2个字符串,消除第一个字符串中第二个字符串包含的所有字母。
: 例如:
: string1: helloworld
: string2: abcdef
: output: hlloworld
: 面试的时候我说了很笨的办法。因为不许allocate新的空间。
: 大家帮我说说最好的解法应该是什么?谢谢了

avatar
M*e
15

Depends the size of the Alphabet, and the length of the second string.
If the alphabet is small (e.g., English char), create a hash table for that
- O(size of alphabet * len_string1). If the second string is small, (e.g.,
1 char), brute force search on the first string - O(len_string1 * len_
string2). If both are large, sort the second string, and go over the first
string while binary look up each char in the sorted second string - O(lg(len
_string2) * (len_string2 + len_string1)).

【在 s*******n 的大作中提到】
: 面试题目:
: 有2个字符串,消除第一个字符串中第二个字符串包含的所有字母。
: 例如:
: string1: helloworld
: string2: abcdef
: output: hlloworld
: 面试的时候我说了很笨的办法。因为不许allocate新的空间。
: 大家帮我说说最好的解法应该是什么?谢谢了

avatar
g*s
16
I replied the 2 lou(who mentioned to use sort+binary search to delete
first) and i agreed. (if string2 is very small, scan could be faster
because of binary search overhead)
After enough chars(26 or 52 or 256) are deleted, we can compact it and
use the deleted space.
All we assume is that string1 is very large.

二十六
复的字母怎
么办?抑或是 string1 产生重复的个别字母不超过 string2 的长度?那这基本上就跟
worst
case 一样,26*n 的比较次数,那还不如把 string2 排好序,然后做 binary search。

【在 i**********e 的大作中提到】
: 我想就是利用两个前后指针扫描,然后首先可以用简单的 linear search 来寻找重复
: 的。直到后指针与前指针的距离为 26,那也意为着有 26 个连续的空位。这时候就把
: 后指针指向和之后的所有元素往前移 26位,然后利用最后的 26 空位来当 hash。
: 也就是 grass 所说的是从 string1 里删除了二十六个重复的字母之后,利用剩下的二十六
: 个空位来当作hash来用。
: 这是考虑到 string1 的长度非常长。但是有一个最坏情况就是万一很久都没碰上重复的字母怎么办?抑或是 string1 产生重复的个别字母不超过 string2 的长度?那这基本上就跟 worst case 一样,26*n 的比较次数,那还不如把 string2 排好序,然后做 binary search。
: 一些常见面试题的答案与总结 -
: http://www.ihas1337code.com

avatar
s*y
17
一个字符8bit,4个字符就有32个bit可以做bitmap hash 26个字符了,这样子可以吗
唯一的就是当第一个字符如果它的bitmap影射到第2个字符,那么在读第2个字符前就会
破坏了第2个字符。
avatar
f*4
18
还有个办法是破坏string2,只要string2有>=4个char,就有32bits,每个bit代表一个
字母;遍历string2剩下字符然后设置string2前26bits。
用这26bits做hashmap,遍历string1
这样就避免了string1 move char
如果string2 size<4; 直接查找即可
avatar
m*l
19
不能用XOR?
helloworld
^aaaaaaaaaa
delete any zero.
^aaaaaaaaaa
...
helloworld
^eeeeeeeeee
delete any zero.
^eeeeeeeeee

【在 s*******n 的大作中提到】
: 面试题目:
: 有2个字符串,消除第一个字符串中第二个字符串包含的所有字母。
: 例如:
: string1: helloworld
: string2: abcdef
: output: hlloworld
: 面试的时候我说了很笨的办法。因为不许allocate新的空间。
: 大家帮我说说最好的解法应该是什么?谢谢了

avatar
d*l
20
感觉有点南辕北辙。。相同的字母xor了确实会变成0,但不同的字母xor结果不就乱了
?除非不同的字母的对应的是全0,但这样意义很小,对效率好像也没什么帮助

【在 m********l 的大作中提到】
: 不能用XOR?
: helloworld
: ^aaaaaaaaaa
: delete any zero.
: ^aaaaaaaaaa
: ...
: helloworld
: ^eeeeeeeeee
: delete any zero.
: ^eeeeeeeeee

avatar
m*l
21

XOR两次
没仔细读.. 不准用新空间
不过他问题也没说好
output跟string1是同个空间吗?
helloworld 是要变成 hlloworl
多了个d

【在 d*******l 的大作中提到】
: 感觉有点南辕北辙。。相同的字母xor了确实会变成0,但不同的字母xor结果不就乱了
: ?除非不同的字母的对应的是全0,但这样意义很小,对效率好像也没什么帮助

avatar
m*l
22
昨晚在外面想了个方法
用bit encoding.
比如说
0001 = a
0010 = b
0100 = c
1000 = d
0001 0000=e
0010 0000=f
...
用26-bit. (4 bytes)
然后
string2 = (1 <int i=0;
while(string1[i])
{
if ((1 << (string1[i]-'a')) & string2 == 0) cout << string[i];
i++;
}

【在 s*******n 的大作中提到】
: 面试题目:
: 有2个字符串,消除第一个字符串中第二个字符串包含的所有字母。
: 例如:
: string1: helloworld
: string2: abcdef
: output: hlloworld
: 面试的时候我说了很笨的办法。因为不许allocate新的空间。
: 大家帮我说说最好的解法应该是什么?谢谢了

avatar
f*4
23
恩,用string2 做mapping比较方便

【在 m********l 的大作中提到】
: 昨晚在外面想了个方法
: 用bit encoding.
: 比如说
: 0001 = a
: 0010 = b
: 0100 = c
: 1000 = d
: 0001 0000=e
: 0010 0000=f
: ...

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。