389. 找不同

题目分析:
将两个字符串按照字母序进行排序,然后从头开始遍历,找出第一个不相等的元素即为所求。

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

猜你喜欢

转载自blog.csdn.net/LITTENg/article/details/80038321