Loading

C语言入门练习-找不同(Leecode 389)

Leecode 389. 找不同

1. 个人解法,使用哈希表映射

char findTheDifference(char* s, char* t) {
    // 第二种 构建哈希表
    int table[26] = {0};
    char res;
    int i = 0;
    while (s[i] != '\0') {
        table[s[i] - 97]++;
        i++;
    }
    // 对于t 在哈希表减去 相同的减去 若哈希表不存在这个字符,则返回。
    int j = 0;
    while (t[j] != '\0') {
        if (table[t[j] - 97]) {
            table[t[j] - 97]--;
        } else {
            res = t[j];
        }
        j++;
    }
    return res;
}

2. 神仙解法,累加求和,取差值的ASCII码

char findTheDifference(char* s, char* t) {
    int tSum = 0;
    for (int i = 0; i < strlen(t); i++) {
        tSum += t[i];
    }

    int sSum = 0;
    for (int i = 0; i < strlen(s); i++) {
        sSum += s[i];
    }

    return tSum - sSum;
}

3.参考链接

题目链接:https://leetcode.cn/problems/find-the-difference/description/?envType=problem-list-v2&envId=14k3kZvB
链接:https://leetcode.cn/problems/find-the-difference/solutions/2587837/389-zhao-bu-tong-by-cool-keldyshbip-ynyp/

posted @ 2025-06-15 22:08  Sunyaa  阅读(7)  评论(0)    收藏  举报