LeetCode之389. Find the Difference

LeetCode之389. Find the Difference

--------------------------------------------------

先计算每个字母的出现次数然后减去,最后剩下的那一个就是后来添加的了。

AC代码:

public class Solution {
public char findTheDifference(String s, String t) {
int book[]=new int[26];
for(int i=0;i<s.length();i++) book[s.charAt(i)-'a']++;
for(int i=0;i<t.length();i++) book[t.charAt(i)-'a']--;
for(int i=0;i<book.length;i++) if(book[i]!=0) return (char)(i+'a');
return ' ';
}
}

题目来源: https://leetcode.com/problems/find-the-difference/

上一篇:LeetCode 389. Find the Difference


下一篇:LeetCode 389. Find the Difference (找到不同)