zoukankan      html  css  js  c++  java
  • C语言字符串/数组去重

    输入: hello 输出: helo

    第一种实现: 不新开数组, 也就是原地去重.

    #include <stdio.h>
    #include <string.h>
    
    void removeDuplicate(char str[]);
    
    int main (void) {
        char name[] = "hello";
        removeDuplicate(name);
        printf("%s
    ", name);
        return 0;
    }
    
    void removeDuplicate(char str[]) {
        int len = strlen(str);
        int p = 0;
        int i;
        int j;
        for (i=0; i<len; i++) {
            if (str[i] != '') {
                str[p++] = str[i];
                for (j=i+1; j<len; j++) {
                    if (str[i] == str[j]) {
                        str[j] = '';
                    }
                }
            }
        }
        str[p] = '';
    }
    

    上面的代码一共出现了3次'', 前2次的''没有什么特殊含义, 可以替换成任何在所给字符串中
    不会出现的字符. 最后一个''则是C语言中特有的, 是字符串结束标志.
    就是把所有重复的元素标记成'', 那么剩下的元素则是不重复的元素, 通过变量p, 把这些元素重新
    添加到结果字符串中即可.

    第二种实现: 新开数组实现.

    #include <stdio.h>
    #include <string.h>
    
    void removeDuplicate(char str[], char res[]);
    
    int main (void) {
        char name[20] = "sdfsssww";
        char res[20];
        removeDuplicate(name, res);
        printf("%s
    ", res);
        return 0;
    }
    
    void removeDuplicate(char str[], char res[]) {
        int slen = strlen(str);
        int rlen = 0;
        int flag;        // 元素重复标志
        int i;
        int j;
        for (i=0; i<slen; i++) {
            flag = 0;
            for (j=0; j<rlen; j++) {
            // 每次都把结果数组遍历一遍, 与当前字符比较, 有重复
            // 就标记为 1
                if (res[j] == str[i]) flag = 1;
            }
            if (flag == 0) {
                res[rlen++] = str[i];
            }
        }
        res[rlen] = '';
    }
    

    第三种, 一层循环, 开个ASCII数组进行标记

    #include <stdio.h>
    #include <string.h>
    
    void removeDuplicate(char str[]);
    
    int main (void) {
        char name[] = "wwwwsssspp";
        removeDuplicate(name);
        printf("%s
    ", name);
        return 0;
    }
    
    void removeDuplicate(char str[]) {
        int len = strlen(str);
        int ascii[128] = {0};
        int p = 0;
        int i;
    
        for (i=0; i<len; i++) {
            if (ascii[str[i]] == 0) {
                ascii[str[i]] = 1;
                str[p++] = str[i];
            }
        }
        str[p] = '';
    }
    

    第四种, 也是新开ASCII数组进行标记, 实现去2重, 比如输入: sswqswww, 输出: sswqsw

    #include <stdio.h>
    #include <string.h>
    
    void removeDuplicate(char str[]);
    
    int main (void) {
        char name[] = "sswqswww";
        removeDuplicate(name);
        printf("%s
    ", name);
        return 0;
    }
    
    void removeDuplicate(char str[]) {
        int len = strlen(str);
        int ascii[128] = {0};
        int p = 0;
        int i;
    
        for (i=0; i<len; i++) {
            if (ascii[str[i]] != 2) {
                ascii[str[i]]++;
                str[p++] = str[i];
            }
        }
        str[p] = '';
    }
    

    第五种, 上面的代码简单改下, 既可以实现去n重

    #include <stdio.h>
    #include <string.h>
    
    void removeDuplicate(char str[], int n)
    
    int main (void) {
        char name[] = "sswqswww";
        removeDuplicate(name, 2);
        printf("%s
    ", name);
        return 0;
    }
    
    void removeDuplicate(char str[], int n) {
        int len = strlen(str);
        int ascii[128] = {0};
        int p = 0;
        int i;
    
        for (i=0; i<len; i++) {
            if (ascii[str[i]] != n) {
                ascii[str[i]]++;
                str[p++] = str[i];
            }
        }
        str[p] = '';
    }
    

    参考链接: http://www.hawstein.com/posts/1.3.html

  • 相关阅读:
    一行代码更改博客园皮肤
    fatal: refusing to merge unrelated histories
    使用 netcat 传输大文件
    linux 命令后台运行
    .net core 使用 Nlog 配置文件
    .net core 使用 Nlog 集成 exceptionless 配置文件
    Mysql不同字符串格式的连表查询
    Mongodb between 时间范围
    VS Code 使用 Debugger for Chrome 调试vue
    css权重说明
  • 原文地址:https://www.cnblogs.com/asheng2016/p/7793026.html
Copyright © 2011-2022 走看看