首页 > 其他 > 详细

LeetCode之389. Find the Difference

时间:2016-10-24 07:39:42      阅读:277      评论:0      收藏:0      [点我收藏+]

技术分享

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

 

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

 

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

原文:http://www.cnblogs.com/cc11001100/p/5991647.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!