力扣389

我的答案:

class Solution {
public:
    char findTheDifference(string s, string t) {
        int s1 = 0, t1 = 0, val = 0;
        for (char ss : s) {
            int asciiValue = static_cast<int>(ss);
            s1 += asciiValue;
        }
        for (char tt : t) {
            int asciiValue = static_cast<int>(tt);
            t1 += asciiValue;
        }
        val = t1 - s1;
        char character = static_cast<char>(val);
        return character;
    }
};

 他人时间最快答案:

class Solution {
public:
    char findTheDifference(string s, string t) {
        int s1 = 0, t1 = 0;
        for (char ss : s) {
            s1 += ss;
        }
        for (char tt : t) {
            t1 += tt;
        }
        return  t1 - s1;
    }
};

 

 另一种思路解法:

class Solution {
public:
    char findTheDifference(string s, string t) {
        int i =0;
        sort(s.begin(),s.end());
        sort(t.begin(),t.end());
        while(s[i]==t[i]){
            i++;
        }
        return t[i];
    }
};

 

posted @ 2024-01-02 14:11  joyfulest  阅读(1)  评论(0编辑  收藏  举报