本文主要是介绍Leetcode——389. Find the Difference,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目
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.
解答
class Solution1 {
public:char findTheDifference(string s, string t) {char res;string st=s+t;int res_i=st[0]-'a';for(int i=1;i<st.length();i++)res_i=res_i^(st[i]-'a');res=res_i+'a';return res;}
};
class Solution2 {
public:char findTheDifference(string s, string t) {char res=0;for(auto c:s) res=res^c;for(auto c:t) res=res^c;return res;}
};
class Solution {
public:char findTheDifference(string s, string t) {int res=0;for(auto c:t) res+=(int)c;for(auto c:s) res-=(int)c;return (char)res;}
};
这篇关于Leetcode——389. Find the Difference的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!