字符串去重

void RemoveDuplicate(char *str)
{
    int i, j, k;
    unsigned long len = strlen(str);
    for(i = k = 0; i < len; i++) {
        if(str[i]) {
            str[k++] = str[i];
            for(j = i + 1; j < len; j++)
                if(str[j] == str[i])
                    str[j] = '\0';
        }
    }
    str[k] = '\0';
}


void RemoveDuplicate1(char *s) {
    char check[256] = { 0 };
    int j = 0;
    unsigned long len = strlen(s);
    for(int i = 0; i < len; i++)  {
        if(check[s[i]] == 0) {
            s[j++] = s[i];
            check[s[i]] = 1;
        }
    }
    s[j] = '\0';
}

void RemoveDuplicate3(char *s) {
    int remainder;
    int check[8] = {0}; //4個(gè)字節(jié) * 8 * 8bit = 256bit
    int j = 0;
    unsigned long len = strlen(s);
    for(int i = 0; i < len; i++)  {
        remainder = s[i] % 32; //remainder 范圍0-31
        //s[i] >> 5是確定在數(shù)組中哪個(gè)位置

        printf("%d -- %d--%d", s[i] >> 5, check[s[i] >> 5], remainder);
        printf("\n");
        if((check[s[i] >> 5] & (1 << remainder)) == 0) {
            s[j++] = s[i];
            check[s[i] >> 5] |= (1 << remainder);
        }
    }
    s[j] = '\0';
}

void RemoveDuplicate2(char *s)
{
    int i, j, val, check;
    j = check = 0;
    for(i = 0; s[i]; i++)
    {
        val = s[i] - 'a';
        if((check & (1 << val)) == 0)
        {
            s[j++] = s[i];
            check |= 1 << val;
        }
    }
    s[j] = '\0';
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容