crack the code interview1.3

本文介绍了一种位图法来去除字符串中的重复字符,不使用额外缓冲区,仅使用一到两个额外变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ++;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值