leetcode 389. Find the Difference

注意,两个字符串顺序可以是乱的。

两种思路:

直接双哈希。异或。

    char findTheDifference(string s, string t) {     
        int ss[26] = {0};
        int tt[26] = {0};
        
        for (auto si : s)
            ss[si - 'a']++;
        for (auto ti : t)
            tt[ti - 'a']++;
               
        for (int i = 0; i < 26; i++)
            if (ss[i] == tt[i] - 1)
               return i + 'a';
    }
    char findTheDifference(string s, string t) {
        char ret = 0;
        
        for (auto si : s)
            ret ^= si;
        for (auto ti : t)
            ret ^= ti;
        return ret;
    }

 

posted on 2018-02-11 11:15  willaty  阅读(166)  评论(0编辑  收藏  举报

导航