avatar
贡献两道的面试题# JobHunting - 待字闺中
a*2
1
1.给定一个数组(包含有重复数),找出所有可能的组合
比方说1,2,2,那么组合有(),(1),(2),(1,2),(2,2),(1,2,2)
2.给定字符的三种存储方式: HH, HL, L
现在给定一个包含H和L的长序列和一个数组地址,要求找出这个地址所在字符的首地址。
i.e. input HLHHHLHHH... 和 6 返回 6 因为 HL|HH|HL|HH|H....
input HLHHHLHHH... 和 7 返回 6 同样因为HL|HH|HL|HH|H...
avatar
s*n
2
1. DP + hashtable
avatar
v*k
3
第二个不懂

址。

【在 a**********2 的大作中提到】
: 1.给定一个数组(包含有重复数),找出所有可能的组合
: 比方说1,2,2,那么组合有(),(1),(2),(1,2),(2,2),(1,2,2)
: 2.给定字符的三种存储方式: HH, HL, L
: 现在给定一个包含H和L的长序列和一个数组地址,要求找出这个地址所在字符的首地址。
: i.e. input HLHHHLHHH... 和 6 返回 6 因为 HL|HH|HL|HH|H....
: input HLHHHLHHH... 和 7 返回 6 同样因为HL|HH|HL|HH|H...

avatar
B*1
4
1不是直接用那种n个数,就loop 1-》 2^n, 看那个bit是1就输出哪个数那样子?
最多搞个hash判断一下重复吧?

【在 s******n 的大作中提到】
: 1. DP + hashtable
avatar
s*n
5
对每个字母从0-count 加进去,然后check下一个字母
char c =KeySet[num];
int count = map.get(c);
recSub(map,stack,KeySet,num+1);
for(int i=0;istack.push(c);
recSub(m,stack,letter,num+1);
}
avatar
l*a
6
why use hashtable
1) sort ==>used for avoid duplication
2) use recursion to get combination

【在 B*******1 的大作中提到】
: 1不是直接用那种n个数,就loop 1-》 2^n, 看那个bit是1就输出哪个数那样子?
: 最多搞个hash判断一下重复吧?

avatar
B*1
7
otherwise, 1,2,2, may output 1,2 twice?

【在 l*****a 的大作中提到】
: why use hashtable
: 1) sort ==>used for avoid duplication
: 2) use recursion to get combination

avatar
S*H
8
1. sort and count, give count_1, count_2, ..., count_p.
Then the total number is (count_1+1)(count_2+1)...(count_p+1).

址。

【在 a**********2 的大作中提到】
: 1.给定一个数组(包含有重复数),找出所有可能的组合
: 比方说1,2,2,那么组合有(),(1),(2),(1,2),(2,2),(1,2,2)
: 2.给定字符的三种存储方式: HH, HL, L
: 现在给定一个包含H和L的长序列和一个数组地址,要求找出这个地址所在字符的首地址。
: i.e. input HLHHHLHHH... 和 6 返回 6 因为 HL|HH|HL|HH|H....
: input HLHHHLHHH... 和 7 返回 6 同样因为HL|HH|HL|HH|H...

avatar
s*6
9
第二道题目没看明白。
avatar
b*t
10
第二个其实相当于是个哈夫曼编码
不过没看出来有什么玄机 就从前往后扫好了 扫到包含input的那个index
自然就知道返回哪个了

【在 v*****k 的大作中提到】
: 第二个不懂
:
: 址。

avatar
b*t
11
看来这个题是需要小于O(n)的算法 想想看

【在 b******t 的大作中提到】
: 第二个其实相当于是个哈夫曼编码
: 不过没看出来有什么玄机 就从前往后扫好了 扫到包含input的那个index
: 自然就知道返回哪个了

avatar
a*2
12
我当时就是这么做的,backtracking

【在 S*****H 的大作中提到】
: 1. sort and count, give count_1, count_2, ..., count_p.
: Then the total number is (count_1+1)(count_2+1)...(count_p+1).
:
: 址。

avatar
a*2
13
对,我也是给了提示才做出来,关键是找特殊的pattern

【在 b******t 的大作中提到】
: 看来这个题是需要小于O(n)的算法 想想看
avatar
a*2
14
如何用hash判断重复?

【在 B*******1 的大作中提到】
: 1不是直接用那种n个数,就loop 1-》 2^n, 看那个bit是1就输出哪个数那样子?
: 最多搞个hash判断一下重复吧?

avatar
b*t
15
每出现一个数a hashmap[a]++;
可以避免sorting

【在 a**********2 的大作中提到】
: 如何用hash判断重复?
avatar
b*t
16
en 往开始的方向前找L L一定是一个结束符
然后就可以了

【在 b******t 的大作中提到】
: 看来这个题是需要小于O(n)的算法 想想看
avatar
l*a
17
void GetCombination(int a[],int size,int start,vector &vec)
{
for(int i=start;i<=size-1;i++)
{
if((i!=start)&&(a[i]==a[i-1])) continue;
vec.push_back(a[i]);
PrintCombination(vec);
if(ivec.pop_back();
}
}

【在 B*******1 的大作中提到】
: otherwise, 1,2,2, may output 1,2 twice?
avatar
B*1
18
ye. I get it.
Thanks.

【在 l*****a 的大作中提到】
: void GetCombination(int a[],int size,int start,vector &vec)
: {
: for(int i=start;i<=size-1;i++)
: {
: if((i!=start)&&(a[i]==a[i-1])) continue;
: vec.push_back(a[i]);
: PrintCombination(vec);
: if(i: vec.pop_back();
: }

avatar
s*e
19
2nd
int findpos(int[] a, int k){
int p=0;
int i=0;
while(i<=k){
p=i;
if(a[i]=='L')
i++;
else
i+=2;
}
return p;
}

【在 b******t 的大作中提到】
: en 往开始的方向前找L L一定是一个结束符
: 然后就可以了

avatar
b*t
20
你这个O(n)了

【在 s******e 的大作中提到】
: 2nd
: int findpos(int[] a, int k){
: int p=0;
: int i=0;
: while(i<=k){
: p=i;
: if(a[i]=='L')
: i++;
: else
: i+=2;

avatar
s*e
21
what is the better algorithm ?

你这个O(n)了

【在 b******t 的大作中提到】
: 你这个O(n)了
avatar
b*t
22
int findpos(int [] a,int k)
{
int p=k-1;
while(p>=0&& a[p]!='L')
p--;
int i=p+1;
while(i<=k)
{
p=i;
if(a[i]='L')
i++;
else
i+=2;
}
return p;
}

【在 s******e 的大作中提到】
: 2nd
: int findpos(int[] a, int k){
: int p=0;
: int i=0;
: while(i<=k){
: p=i;
: if(a[i]=='L')
: i++;
: else
: i+=2;

avatar
s*e
23
how about this
int findpos(int[] a,int k) {
int p =k-1;
while(p>=0 && a[p]!='L')
p--;
if( (k-p)%2==0)
return k-1;
else
return k;
}

【在 b******t 的大作中提到】
: int findpos(int [] a,int k)
: {
: int p=k-1;
: while(p>=0&& a[p]!='L')
: p--;
: int i=p+1;
: while(i<=k)
: {
: p=i;
: if(a[i]='L')

avatar
r*g
24
这道题的解法能不能详细讲下
貌似你先对a排序了,这样选择的时候就可以避免重复?我觉得上面那个(a1+1)(a2+1)
。。。这样的解法还好些。你这个循环里面不断的recursion,最后还vec.pop_back,
没看明白
谢谢了

【在 l*****a 的大作中提到】
: void GetCombination(int a[],int size,int start,vector &vec)
: {
: for(int i=start;i<=size-1;i++)
: {
: if((i!=start)&&(a[i]==a[i-1])) continue;
: vec.push_back(a[i]);
: PrintCombination(vec);
: if(i: vec.pop_back();
: }

avatar
a*2
25
就是这个意思,我当时脑子不转了被提示了半天才想到,真是功力不够

【在 s******e 的大作中提到】
: how about this
: int findpos(int[] a,int k) {
: int p =k-1;
: while(p>=0 && a[p]!='L')
: p--;
: if( (k-p)%2==0)
: return k-1;
: else
: return k;
: }

avatar
r*g
26
睡了一觉起来发现看懂了,收藏这种方法了,我一直都是坚持(1+a1)(1+a2)....或者1-
2^n那种算法。

【在 l*****a 的大作中提到】
: void GetCombination(int a[],int size,int start,vector &vec)
: {
: for(int i=start;i<=size-1;i++)
: {
: if((i!=start)&&(a[i]==a[i-1])) continue;
: vec.push_back(a[i]);
: PrintCombination(vec);
: if(i: vec.pop_back();
: }

avatar
s*f
27

void GetAllComb_(char *str, vector *ret){
static vector vc;
if (!*str){
ret->push_back(string(vc.begin(), vc.end()));
return;
}
char *p = str + 1;
while (*p == *str)
++p;
GetAllComb_(p, ret);
for (int i = 0; i < p - str; ++i){
vc.push_back(*str);
GetAllComb_(p, ret);
}
while (!vc.empty() && vc.back() == *str)
vc.pop_back();
}
void GetAllComb(char *str, vector *ret){
if (!str)
return;
if (!*str){
ret->push_back(string());
return;
}
int len = strlen(str);
sort(str, str + len);
GetAllComb_(str, ret);
}

【在 a**********2 的大作中提到】
: 1.给定一个数组(包含有重复数),找出所有可能的组合
: 比方说1,2,2,那么组合有(),(1),(2),(1,2),(2,2),(1,2,2)
: 2.给定字符的三种存储方式: HH, HL, L
: 现在给定一个包含H和L的长序列和一个数组地址,要求找出这个地址所在字符的首地址。
: i.e. input HLHHHLHHH... 和 6 返回 6 因为 HL|HH|HL|HH|H....
: input HLHHHLHHH... 和 7 返回 6 同样因为HL|HH|HL|HH|H...

avatar
q*x
28

假设unique value x[i],每个有k[i]个,变换成选择问题。每个x[i]有k[i]+1种选法。
址。
要点是正确识别变长字符。扫描时记住每个字符的首地址。

【在 a**********2 的大作中提到】
: 1.给定一个数组(包含有重复数),找出所有可能的组合
: 比方说1,2,2,那么组合有(),(1),(2),(1,2),(2,2),(1,2,2)
: 2.给定字符的三种存储方式: HH, HL, L
: 现在给定一个包含H和L的长序列和一个数组地址,要求找出这个地址所在字符的首地址。
: i.e. input HLHHHLHHH... 和 6 返回 6 因为 HL|HH|HL|HH|H....
: input HLHHHLHHH... 和 7 返回 6 同样因为HL|HH|HL|HH|H...

avatar
a*2
29
1.给定一个数组(包含有重复数),找出所有可能的组合
比方说1,2,2,那么组合有(),(1),(2),(1,2),(2,2),(1,2,2)
2.给定字符的三种存储方式: HH, HL, L
现在给定一个包含H和L的长序列和一个数组地址,要求找出这个地址所在字符的首地址。
i.e. input HLHHHLHHH... 和 6 返回 6 因为 HL|HH|HL|HH|H....
input HLHHHLHHH... 和 7 返回 6 同样因为HL|HH|HL|HH|H...
avatar
s*n
30
1. DP + hashtable
avatar
v*k
31
第二个不懂

址。

【在 a**********2 的大作中提到】
: 1.给定一个数组(包含有重复数),找出所有可能的组合
: 比方说1,2,2,那么组合有(),(1),(2),(1,2),(2,2),(1,2,2)
: 2.给定字符的三种存储方式: HH, HL, L
: 现在给定一个包含H和L的长序列和一个数组地址,要求找出这个地址所在字符的首地址。
: i.e. input HLHHHLHHH... 和 6 返回 6 因为 HL|HH|HL|HH|H....
: input HLHHHLHHH... 和 7 返回 6 同样因为HL|HH|HL|HH|H...

avatar
B*1
32
1不是直接用那种n个数,就loop 1-》 2^n, 看那个bit是1就输出哪个数那样子?
最多搞个hash判断一下重复吧?

【在 s******n 的大作中提到】
: 1. DP + hashtable
avatar
s*n
33
对每个字母从0-count 加进去,然后check下一个字母
char c =KeySet[num];
int count = map.get(c);
recSub(map,stack,KeySet,num+1);
for(int i=0;istack.push(c);
recSub(m,stack,letter,num+1);
}
avatar
l*a
34
why use hashtable
1) sort ==>used for avoid duplication
2) use recursion to get combination

【在 B*******1 的大作中提到】
: 1不是直接用那种n个数,就loop 1-》 2^n, 看那个bit是1就输出哪个数那样子?
: 最多搞个hash判断一下重复吧?

avatar
B*1
35
otherwise, 1,2,2, may output 1,2 twice?

【在 l*****a 的大作中提到】
: why use hashtable
: 1) sort ==>used for avoid duplication
: 2) use recursion to get combination

avatar
S*H
36
1. sort and count, give count_1, count_2, ..., count_p.
Then the total number is (count_1+1)(count_2+1)...(count_p+1).

址。

【在 a**********2 的大作中提到】
: 1.给定一个数组(包含有重复数),找出所有可能的组合
: 比方说1,2,2,那么组合有(),(1),(2),(1,2),(2,2),(1,2,2)
: 2.给定字符的三种存储方式: HH, HL, L
: 现在给定一个包含H和L的长序列和一个数组地址,要求找出这个地址所在字符的首地址。
: i.e. input HLHHHLHHH... 和 6 返回 6 因为 HL|HH|HL|HH|H....
: input HLHHHLHHH... 和 7 返回 6 同样因为HL|HH|HL|HH|H...

avatar
s*6
37
第二道题目没看明白。
avatar
b*t
38
第二个其实相当于是个哈夫曼编码
不过没看出来有什么玄机 就从前往后扫好了 扫到包含input的那个index
自然就知道返回哪个了

【在 v*****k 的大作中提到】
: 第二个不懂
:
: 址。

avatar
b*t
39
看来这个题是需要小于O(n)的算法 想想看

【在 b******t 的大作中提到】
: 第二个其实相当于是个哈夫曼编码
: 不过没看出来有什么玄机 就从前往后扫好了 扫到包含input的那个index
: 自然就知道返回哪个了

avatar
a*2
40
我当时就是这么做的,backtracking

【在 S*****H 的大作中提到】
: 1. sort and count, give count_1, count_2, ..., count_p.
: Then the total number is (count_1+1)(count_2+1)...(count_p+1).
:
: 址。

avatar
a*2
41
对,我也是给了提示才做出来,关键是找特殊的pattern

【在 b******t 的大作中提到】
: 看来这个题是需要小于O(n)的算法 想想看
avatar
a*2
42
如何用hash判断重复?

【在 B*******1 的大作中提到】
: 1不是直接用那种n个数,就loop 1-》 2^n, 看那个bit是1就输出哪个数那样子?
: 最多搞个hash判断一下重复吧?

avatar
b*t
43
每出现一个数a hashmap[a]++;
可以避免sorting

【在 a**********2 的大作中提到】
: 如何用hash判断重复?
avatar
b*t
44
en 往开始的方向前找L L一定是一个结束符
然后就可以了

【在 b******t 的大作中提到】
: 看来这个题是需要小于O(n)的算法 想想看
avatar
l*a
45
void GetCombination(int a[],int size,int start,vector &vec)
{
for(int i=start;i<=size-1;i++)
{
if((i!=start)&&(a[i]==a[i-1])) continue;
vec.push_back(a[i]);
PrintCombination(vec);
if(ivec.pop_back();
}
}

【在 B*******1 的大作中提到】
: otherwise, 1,2,2, may output 1,2 twice?
avatar
B*1
46
ye. I get it.
Thanks.

【在 l*****a 的大作中提到】
: void GetCombination(int a[],int size,int start,vector &vec)
: {
: for(int i=start;i<=size-1;i++)
: {
: if((i!=start)&&(a[i]==a[i-1])) continue;
: vec.push_back(a[i]);
: PrintCombination(vec);
: if(i: vec.pop_back();
: }

avatar
s*e
47
2nd
int findpos(int[] a, int k){
int p=0;
int i=0;
while(i<=k){
p=i;
if(a[i]=='L')
i++;
else
i+=2;
}
return p;
}

【在 b******t 的大作中提到】
: en 往开始的方向前找L L一定是一个结束符
: 然后就可以了

avatar
b*t
48
你这个O(n)了

【在 s******e 的大作中提到】
: 2nd
: int findpos(int[] a, int k){
: int p=0;
: int i=0;
: while(i<=k){
: p=i;
: if(a[i]=='L')
: i++;
: else
: i+=2;

avatar
s*e
49
what is the better algorithm ?

你这个O(n)了

【在 b******t 的大作中提到】
: 你这个O(n)了
avatar
b*t
50
int findpos(int [] a,int k)
{
int p=k-1;
while(p>=0&& a[p]!='L')
p--;
int i=p+1;
while(i<=k)
{
p=i;
if(a[i]='L')
i++;
else
i+=2;
}
return p;
}

【在 s******e 的大作中提到】
: 2nd
: int findpos(int[] a, int k){
: int p=0;
: int i=0;
: while(i<=k){
: p=i;
: if(a[i]=='L')
: i++;
: else
: i+=2;

avatar
s*e
51
how about this
int findpos(int[] a,int k) {
int p =k-1;
while(p>=0 && a[p]!='L')
p--;
if( (k-p)%2==0)
return k-1;
else
return k;
}

【在 b******t 的大作中提到】
: int findpos(int [] a,int k)
: {
: int p=k-1;
: while(p>=0&& a[p]!='L')
: p--;
: int i=p+1;
: while(i<=k)
: {
: p=i;
: if(a[i]='L')

avatar
r*g
52
这道题的解法能不能详细讲下
貌似你先对a排序了,这样选择的时候就可以避免重复?我觉得上面那个(a1+1)(a2+1)
。。。这样的解法还好些。你这个循环里面不断的recursion,最后还vec.pop_back,
没看明白
谢谢了

【在 l*****a 的大作中提到】
: void GetCombination(int a[],int size,int start,vector &vec)
: {
: for(int i=start;i<=size-1;i++)
: {
: if((i!=start)&&(a[i]==a[i-1])) continue;
: vec.push_back(a[i]);
: PrintCombination(vec);
: if(i: vec.pop_back();
: }

avatar
a*2
53
就是这个意思,我当时脑子不转了被提示了半天才想到,真是功力不够

【在 s******e 的大作中提到】
: how about this
: int findpos(int[] a,int k) {
: int p =k-1;
: while(p>=0 && a[p]!='L')
: p--;
: if( (k-p)%2==0)
: return k-1;
: else
: return k;
: }

avatar
r*g
54
睡了一觉起来发现看懂了,收藏这种方法了,我一直都是坚持(1+a1)(1+a2)....或者1-
2^n那种算法。

【在 l*****a 的大作中提到】
: void GetCombination(int a[],int size,int start,vector &vec)
: {
: for(int i=start;i<=size-1;i++)
: {
: if((i!=start)&&(a[i]==a[i-1])) continue;
: vec.push_back(a[i]);
: PrintCombination(vec);
: if(i: vec.pop_back();
: }

avatar
s*f
55

void GetAllComb_(char *str, vector *ret){
static vector vc;
if (!*str){
ret->push_back(string(vc.begin(), vc.end()));
return;
}
char *p = str + 1;
while (*p == *str)
++p;
GetAllComb_(p, ret);
for (int i = 0; i < p - str; ++i){
vc.push_back(*str);
GetAllComb_(p, ret);
}
while (!vc.empty() && vc.back() == *str)
vc.pop_back();
}
void GetAllComb(char *str, vector *ret){
if (!str)
return;
if (!*str){
ret->push_back(string());
return;
}
int len = strlen(str);
sort(str, str + len);
GetAllComb_(str, ret);
}

【在 a**********2 的大作中提到】
: 1.给定一个数组(包含有重复数),找出所有可能的组合
: 比方说1,2,2,那么组合有(),(1),(2),(1,2),(2,2),(1,2,2)
: 2.给定字符的三种存储方式: HH, HL, L
: 现在给定一个包含H和L的长序列和一个数组地址,要求找出这个地址所在字符的首地址。
: i.e. input HLHHHLHHH... 和 6 返回 6 因为 HL|HH|HL|HH|H....
: input HLHHHLHHH... 和 7 返回 6 同样因为HL|HH|HL|HH|H...

avatar
q*x
56

假设unique value x[i],每个有k[i]个,变换成选择问题。每个x[i]有k[i]+1种选法。
址。
要点是正确识别变长字符。扫描时记住每个字符的首地址。

【在 a**********2 的大作中提到】
: 1.给定一个数组(包含有重复数),找出所有可能的组合
: 比方说1,2,2,那么组合有(),(1),(2),(1,2),(2,2),(1,2,2)
: 2.给定字符的三种存储方式: HH, HL, L
: 现在给定一个包含H和L的长序列和一个数组地址,要求找出这个地址所在字符的首地址。
: i.e. input HLHHHLHHH... 和 6 返回 6 因为 HL|HH|HL|HH|H....
: input HLHHHLHHH... 和 7 返回 6 同样因为HL|HH|HL|HH|H...

avatar
x*7
57
没看懂,能解释下思路?printcombination是打印整个vector?

1-

【在 r*******g 的大作中提到】
: 睡了一觉起来发现看懂了,收藏这种方法了,我一直都是坚持(1+a1)(1+a2)....或者1-
: 2^n那种算法。

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