crack the code interview1.3

周辉
2023-12-01

Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not.

这题,只需要像1.1一个保存一个重复字符就行了。写一个位图法的。

void deleteDuplicateChars(char * chr)
{
    int checker = 0;
    char * start = chr;
    while (*start != '\0')
    {
        int value = *chr - 'A';
        if (checker & (1<<value) > 0)
        {
            char * newchr = chr;
            while (*newchr != '\0' && *(newchr + 1) != '\0')
            {
                *newchr = *(newchr + 1);
                newchr ++;
            }
            *newchr = '\0';
        }
        else
            checker |= (1<<value);
        start ++;
    }
}


 类似资料:

相关阅读

相关文章

相关问答