Find the Difference思路:建立一个长度26的数组,循环字符串s和t即可。
GitHub地址:https://github.com/corpsepiges/leetcode
public class Solution {
public char findTheDifference(String s, String t) {
int length=s.length();
char[] cs=s.toCharArray();
char[] ct=t.toCharArray();
int[] nums=new int[26];
nums[ct[length]-'a']++;
for (int i = 0; i < length; i++) {
nums[cs[i]-'a']--;
nums[ct[i]-'a']++;
}
for (int i = 0; i < nums.length; i++) {
if (nums[i]!=0) {
return (char) ('a'+i);
}
}
return 0;
}
}