<LeetCode OJ> 389. Find the Difference

版权声明:本文为EbowTang原创文章,后续可能继续更新本文。如果转载,请务必复制本文末尾的信息! https://blog.csdn.net/EbowTang/article/details/52388207

Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

Example:

Input:
s = "abcd"
t = "abcde"

Output:
e

Explanation:
'e' is the letter that was added.

分析:

题意是从t中找出多增加的那一个字符。本问题还是比较简单的,两个字符串先排序,再挨个遍历,遇到不同的位置显然就找到了。


class Solution {
public:
    char findTheDifference(string s, string t) {
        sort(s.begin(),s.end());
        sort(t.begin(),t.end());
        int pos=0;
        while( pos < t.size() && pos < s.size() )
        {
            if(t[pos]==s[pos])
                pos++;
            else
                break;
        }
        return t[pos];
    }
};



注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/52388207

原作者博客:http://blog.csdn.net/ebowtang

本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895

猜你喜欢

转载自blog.csdn.net/EbowTang/article/details/52388207