avatar
鸡们在跳钢管舞# Joke - 肚皮舞运动
h*g
1
You are given a string like "aaaabbbcc", do an in place conversion which
write frequency of each charater(which come continuosly) with that character
Example:
input: aaabbbccd
output: a3b2c2d1
input: aabaa
output: a2b1a2
avatar
j*e
2
红色食用色素究竟对健康有害没有?
avatar
g*y
3
如果全是不同的单字母,比如abab..., output就是a1b1a1b1..., 需要2N空间,没办法
in-place.

character

【在 h*****g 的大作中提到】
: You are given a string like "aaaabbbcc", do an in place conversion which
: write frequency of each charater(which come continuosly) with that character
: Example:
: input: aaabbbccd
: output: a3b2c2d1
: input: aabaa
: output: a2b1a2

avatar
s*n
4

应该还好

【在 j****e 的大作中提到】
: 红色食用色素究竟对健康有害没有?
avatar
l*8
5
的确是。。。 我怎么没想到。
如果不考虑空间的话,这道题目就是一个读指针一个写指针。 如果写指针不超过读指
针,就可以inplace而且是一遍扫描完成。。 可是下面的例子怎么完成?
ababaaaaaaabbbbbbbabab
a1b1a7b7a1b1
这个例子就难以保证一遍扫描了。

【在 g**********y 的大作中提到】
: 如果全是不同的单字母,比如abab..., output就是a1b1a1b1..., 需要2N空间,没办法
: in-place.
:
: character

avatar
p*g
6
如果在国内,还是“专家”说了蒜

【在 j****e 的大作中提到】
: 红色食用色素究竟对健康有害没有?
avatar
l*8
7
如果array长度是2*n, 但是只有前n个有字母的话,可以反向读取,把结果放到最末尾
的空白空间,最后再向前移到开头。
现在考虑,如果array末尾没有多余空位,n个元素放在长度为n的array里面,而消减后
的字符串保证能放在原来的array里,也还有些tricky.比如刚才说这个例子,
ababaaaaaaabbbbbbbabab
a1b1a1b1a7b7a1b1a1b1

【在 l*********8 的大作中提到】
: 的确是。。。 我怎么没想到。
: 如果不考虑空间的话,这道题目就是一个读指针一个写指针。 如果写指针不超过读指
: 针,就可以inplace而且是一遍扫描完成。。 可是下面的例子怎么完成?
: ababaaaaaaabbbbbbbabab
: a1b1a7b7a1b1
: 这个例子就难以保证一遍扫描了。

avatar
r*e
8
使用跳字,牵强

【在 j****e 的大作中提到】
: 红色食用色素究竟对健康有害没有?
avatar
p*2
9
这题有人要求in place吗?
avatar
l*8
10
in place我觉得更有意思些:)

【在 p*****2 的大作中提到】
: 这题有人要求in place吗?
avatar
p*2
11

这题原题的要求是压缩,如果结果的字符串超过原串的长度则do nothing。可以先计算
一下结果字符串长度。然后in place。

【在 l*********8 的大作中提到】
: in place我觉得更有意思些:)
avatar
p*2
12
不过这个case有些麻烦
caaab
需要先占用字符。不知道还有没有其他的case要特殊处理的。
avatar
l*8
13
如果是压缩的话, ab就不用转换成a1b1了。
ababaaaaaaabbbbbbbabab
就转换成:
ababa7b7abab

【在 p*****2 的大作中提到】
: 不过这个case有些麻烦
: caaab
: 需要先占用字符。不知道还有没有其他的case要特殊处理的。

avatar
l*8
14
恩,你这个例子跟我说的类似,不过更简洁些:)

【在 p*****2 的大作中提到】
: 不过这个case有些麻烦
: caaab
: 需要先占用字符。不知道还有没有其他的case要特殊处理的。

avatar
l*8
15
如果caaab要转换成c1a3b1的话,
可以用三个指针。
第一个i 预扫描,计算到目前为止,是否有足够的space存储要求的字符串,并累计end
_position
第二个指针 j 从 i开始逆向读,
第三个指针从 end_position 开始逆向写

【在 p*****2 的大作中提到】
: 不过这个case有些麻烦
: caaab
: 需要先占用字符。不知道还有没有其他的case要特殊处理的。

avatar
h*d
16
if you are not required to print the "1", you should be able to do that, rig
ht... like a3b2c2d

character

【在 h*****g 的大作中提到】
: You are given a string like "aaaabbbcc", do an in place conversion which
: write frequency of each charater(which come continuosly) with that character
: Example:
: input: aaabbbccd
: output: a3b2c2d1
: input: aabaa
: output: a2b1a2

avatar
h*l
17
我觉得走3遍就好了
第一次把所有不是单独出现的字符写好,
比如 caaab变成 ca3b
然后走一遍 ca3b,看看有多少字符是没有数字1的
然后倒过来写,不是数字加字幕的,就加一个1

rig

【在 h****d 的大作中提到】
: if you are not required to print the "1", you should be able to do that, rig
: ht... like a3b2c2d
:
: character

avatar
p*2
18

rig
原题是u要求a1的。

【在 h****d 的大作中提到】
: if you are not required to print the "1", you should be able to do that, rig
: ht... like a3b2c2d
:
: character

avatar
h*d
19
i was asked the same question in an amazon interview... in that one, the 1 i
s not required and the interviewer hinted that i could do it in place...

【在 p*****2 的大作中提到】
:
: rig
: 原题是u要求a1的。

avatar
w*x
20
先走一遍计算需要空间, 然后倒着来一趟
avatar
p*2
21

i
那不是走一遍就可以了?

【在 h****d 的大作中提到】
: i was asked the same question in an amazon interview... in that one, the 1 i
: s not required and the interviewer hinted that i could do it in place...

avatar
w*o
22
能否讲讲,这个"倒着来一趟"是干什么的?
谢谢!

【在 w****x 的大作中提到】
: 先走一遍计算需要空间, 然后倒着来一趟
avatar
l*8
23
How do you apply your algorithm on
ababaaaaaaabbbbbbbabab
or
caaab ?

【在 w****x 的大作中提到】
: 先走一遍计算需要空间, 然后倒着来一趟
avatar
w*o
24
这道题还有一个tricky的地方,就是要用到类似itoa()的,有的字符可能重复几十次,
可能要占用多余一个字符的位置去存。

character

【在 h*****g 的大作中提到】
: You are given a string like "aaaabbbcc", do an in place conversion which
: write frequency of each charater(which come continuosly) with that character
: Example:
: input: aaabbbccd
: output: a3b2c2d1
: input: aabaa
: output: a2b1a2

avatar
w*x
25

没仔细看, 既然没要求in-place就别费那个脑筋了.

【在 l*********8 的大作中提到】
: How do you apply your algorithm on
: ababaaaaaaabbbbbbbabab
: or
: caaab ?

avatar
p*2
26

几百,几千,几万次都又可能吧?

【在 w****o 的大作中提到】
: 这道题还有一个tricky的地方,就是要用到类似itoa()的,有的字符可能重复几十次,
: 可能要占用多余一个字符的位置去存。
:
: character

avatar
s*r
27
只处理出现次数小于10的情况,但是可以很容易修改处理出现次数多余9次的情况。
我写的,见笑了:
/***********************************************************************
*********************/
/* Compress a String: AAABBCDDDD will be A3B2CD4 */
//
private static String compressString(String str)
{
char[] chars = str.toCharArray();
char current = chars[0];
int count = 1;
int indexCounter = 1;
int shiftBack = 0;
for(int i = 1; i< chars.length; i++){
if (current == chars[i]){
count++;
chars[indexCounter] = Integer.toString(count).charAt(0);
}
else
{
current = chars[i];
//shift array back to fill up any vacant slots
shiftBack += count > 2 ? (count-2) : 0;
//reset count to start next group
count = 1;
if (shiftBack > 0)
{
chars[i - shiftBack] = chars[i];
}
indexCounter = i - shiftBack + 1;
}
}
return new String(chars,0, (indexCounter + (count > 1 ? 1 : 0)) );
}
avatar
i*7
28
走一遍计算空间,如果要求空间大于所指定空间。
realloc一下。我觉得这也算inplace的。
avatar
i*7
29
补充一下上面的:然后从最后往前写。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。