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.
输入给两个只包含小写字母的字符串 S 和 T.
字符串 T 是在字符串 S 基础上添加一个小写字母, 然后随机打乱之后生成.
请找出那个被加入的小写字母.
示例:
Input:
s = "abcd"
t = "abcde"
Output:
e
Explanation:
'e' is the letter that was added.
在线测试网站: https://leetcode.com/problems/find-the-difference/description
class Solution {
public char findTheDifference(String s, String t) {
int ret = 0;
for(int i = 0; i < s.length(); i++) {
ret -= (int) s.charAt(i);
}
for(int i = 0; i < t.length(); i++) {
ret += (int) t.charAt(i);
}
return (char) ret;
}
}
网友评论