【在 l*********8 的大作中提到】 : Here bufLength is the length of the 内存空间, not the string.
I*l
16 楼
I am confused here: it seems you may erase some of the original characters in the string. For example, if newLength < length, then starting from the end, you will overwrite the characters that have not been processed.
【在 l*********8 的大作中提到】 : bool Convert(char * buf, int bufSize) : { : int length = strlen(buf); : int newLength = length; : for (int i=0; i: if (buf[i] == 'A') : newLength--; : else if(buf[i] == 'B') : newLength++; : if (newLength > bufSize - 1) return false;
l*8
17 楼
good catch! thanks!
【在 I*******l 的大作中提到】 : I am confused here: it seems you may erase some of the original characters : in the string. For example, if newLength < length, then starting from the : end, you will overwrite the characters that have not been processed.
【在 g*******4 的大作中提到】 : 处理一个字符串,删除里面所有的A,double所有的B : 例子,输入 CAABD, 输出是CBBD : 要求in space , O (1), no extra memory cost,因为字符串处理变长的空间不算
t*t
31 楼
第一遍处理A, 同时数B 第二遍处理B
to
【在 D********g 的大作中提到】 : 我的code: : 先扫一遍找到A的个数和B的个数 : 然后再扫一遍处理A : 再扫一遍处理B : code里处理A和B的算法不太一样,第二种更好一点。另外,有没有包子? : /*input is assumed to end at '\0' and input is assumed to be long enough to : hold the : * transformed string : * */ : static char[] deleteADoubleB(final char[] input) {
r*k
32 楼
void deleteADoubleB(char *str, int length) { if (str == NULL || length < 0) return;
int newLen = 0; int bCount = 0; for (int i = 0; i < str[i] !='\0'; i++) { if (str[i] != 'A') { str[newLen++] = str[i]; } if (str[i] == 'B') { bCount++; } } if (newLen + bCount > length) return; int i = newLen - 1; newLen += bCount; str[newLen--] = '\0'; while (i>0) { str[newLen--] = str[i]; if (str[i] == 'B') { str[newLen--] = 'B'; } i--; } }
s*e
33 楼
Test Case: ABCCCCCCCCCCCCCCC nothing wrong, but there is unnecessary shift in your algorithm.
【在 r****k 的大作中提到】 : void deleteADoubleB(char *str, int length) : { : if (str == NULL || length < 0) return; : : int newLen = 0; : int bCount = 0; : for (int i = 0; i < str[i] !='\0'; i++) : { : if (str[i] != 'A') : {
t*h
34 楼
谢谢。 看来是把150题两道原题给揉在一起了。给一个string,删掉特定的字符。比如“I am a student"删”aeiou".第二个,把一个string里所有的space替换成20%
【在 g*******4 的大作中提到】 : 处理一个字符串,删除里面所有的A,double所有的B : 例子,输入 CAABD, 输出是CBBD : 要求in space , O (1), no extra memory cost,因为字符串处理变长的空间不算
s*f
35 楼
i only have scan 2 times version. 1st scan for to delete A and count B. 2nd scan double b from the end that we calculated from 1st scan.
【在 g*******4 的大作中提到】 : 处理一个字符串,删除里面所有的A,double所有的B : 例子,输入 CAABD, 输出是CBBD : 要求in space , O (1), no extra memory cost,因为字符串处理变长的空间不算
o*o
36 楼
public class StringProcessor { //return new length //assume ary is large enough public static int process(char[] ary, int length) { int i=0, j=-1; int countB = 0; while(i < length) { if(j == -1) { if(ary[i] == 'A') j = i; } else { if(ary[i] != 'A') { ary[j] = ary[i]; j++; } } if(ary[i] == 'B') countB++; i++; } //j == new length if(countB > 0) { int n = j + countB - 1; i = j-1; while(i >= 0) { ary[n] = ary[i]; n--; if(ary[i] == 'B') { ary[n] = ary[i]; n--; } i--; } } return j + countB; } }
【在 w****x 的大作中提到】 : 第一趟从左到右去A数B, : 第二趟从右到左double B
o*o
37 楼
改进一下 public class StringProcessor { //return new length //assume ary is large enough public static int process(char[] ary, int length) { int i=0, j=0; int countB = 0; while(i < length) { char c = ary[i++]; if(c != 'A') ary[j++] = c; if(c == 'B') countB++; } //j == new length if(countB > 0) { int n = j + countB - 1; i = j - 1; while(i >= 0) { char c = ary[i--]; ary[n--] = c; if(c == 'B') ary[n--] = c; } } return j + countB; }
【在 o****o 的大作中提到】 : public class StringProcessor { : //return new length : //assume ary is large enough : public static int process(char[] ary, int length) { : int i=0, j=-1; : int countB = 0; : while(i < length) { : if(j == -1) { : if(ary[i] == 'A') : j = i;
t*j
38 楼
nod,扫描两遍,第一遍数数计算 output string长度。 第二遍从后向前移动处理。
【在 c****p 的大作中提到】 : 把字符串从后往前移
y*u
39 楼
这就不是O(1)了啊
【在 w****x 的大作中提到】 : 第一趟从左到右去A数B, : 第二趟从右到左double B
m*y
40 楼
同意楼上peking2。从前往后,去A,从后往前,double B。 C代码如下: #include #define MAX_S_LENGTH 1024 void remove_a_double_b(char *s) { int i, j, n, na, nb, nc; // nc is number of characters other than A na = nb = 0; j = 0; for (i = 0; s[i] != '\0'; i++) { if (s[i] == 'A') na++; else { s[j] = s[i]; j++; if (s[i] == 'B') nb++; } } n = i; nc = j; i = n - na + nb; s[i] = '\0'; i--; for (j = nc - 1; j >= 0; j--) { if (s[j] == 'B') { s[i] = 'B'; i--; s[i] = 'B'; i--; } else { s[i] = s[j]; i--; } } } void main() { char s[MAX_S_LENGTH]; scanf("%s", s); remove_a_double_b(s); printf("%s\n", s); }
【在 p*****2 的大作中提到】 : 从前往后再从后往前就可以了吧。
J*9
41 楼
char *hkStringRemoveADoubleB(char *str) { if (!str) return NULL; char *s=str; char *d=str; int countb = 0; int len=0; while(*s) { if (*s=='B') countb++; /// Count B if(*s!='A') /// Remove A { *d++=*s++; len++; } else s++; } if(len) { s = str+(len+countb); ///New string length *s = '\0'; s--; d--; while(d!=str) { if (*d=='B') *s-- = 'B'; *s--=*d--; } if (*d=='B') /// If the first one is B *s = 'B'; } return str; } Test cases: str1=CAABD str1=CBBD str1=ACAABDB str1=CBBDBB str1=BACAABDBA str1=BBCBBDBB str1=ABCCCCCCCCCCCCCCC str1=BBCCCCCCCCCCCCCCC