原题

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.

题目要求

题目叫“找出不同点”,题目本身也比较简单。有两个字符串s和t,它们只包含小写的字母。字符串t是由字符串s产生的,只比s多一个字母。题目的目的就是要找出这个多出的字母。

解法

解法一:这种解法比较直接,建立以个map对应字符串s,key为字符,value就是该字符出现的次数。首先遍历字符串s,来建立这个map,然后再遍历字符串t。对t中出现的每个字符,都从map中减一,当value值小于0时,则说明该字符就是多出的字符。代码我就不列出来了,这种解法和第二种类似,大家看了第二种解法就明白了。

解法二:第一种解法的map有点太重了,而且涉及到Character和char,int和Integer之间的转换,我也不喜欢。所以,我就用了另一种很类似的方法——int数组来代替map。因为字符串只有小写字母,也就是只有26中可能,那么建立一个int[16]的数组即可,索引就是字符char-'a',而数组值就是字符出现在字符串s中的次数。说白了,和解法二思路一致。代码如下:

public char findTheDifference(String s, String t) {
int[] nums = new int[26];
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
++nums[ch - 'a'];
}
char ret = 'a';
for (int i = 0; i < t.length(); i++) {
char ch = t.charAt(i);
--nums[ch - 'a'];
if (nums[ch - 'a'] < 0) {
ret = ch;
break;
}
}
return ret;
}

解法三:由于字符串t只比字符串s多了一个字符,那么直接用t中所有字符值的和减去字符串s中字符值的和即可。

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;
}

解法四:另外一种思路,既然字符串t只比字符串s多了一个字符,也就是说大部分字符都是相同的。那么,我们可以使用异或的方式,来找出这个不同的值。

public char findTheDifference(String s, String t) {
int ret = 0;
for (int i = 0; i < s.length(); i++) {
ret ^= s.charAt(i);
}
for (int i = 0; i < t.length(); i++) {
ret ^= t.charAt(i);
}
return (char)ret;
}

【LeetCode】389 Find the Difference(java)的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  2. 【LeetCode】10.Regular Expression Matching(dp)

    [题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...

  3. 【LeetCode】TreeNode类实现解析(java实现)

    https://blog.csdn.net/styshoo/article/details/52865386 在LeetCode中,TreeNode是经常用到的一个结构体,表示数据结构树(Tree)中 ...

  4. 【LeetCode】389. Find the Difference 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:异或 方法三:排序 日 ...

  5. 【leetcode】Search for a Range(middle)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  6. 【LeetCode】51. N-Queens 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  7. 【LeetCode】274. H-Index 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index/ ...

  8. 【leetcode】Swap Nodes in Pairs (middle)

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  9. 【leetcode】Reverse Nodes in k-Group (hard)☆

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

随机推荐

  1. mac下搭建lua环境

    mac下安装lua(可借助:rudix 地址:http://rudix.org) curl -s https://raw.githubusercontent.com/rudix-mac/rpm/201 ...

  2. Android开发小问题总结

    Android开发遇到的小问题之小解: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  3. atitit.js浏览器环境下的全局异常捕获

    atitit.js浏览器环境下的全局异常捕获 window.onerror = function(errorMessage, scriptURI, lineNumber) { var s= JSON. ...

  4. 大数据时代的IT架构设计

    大数据时代的IT架构设计(来自互联网.银行等领域的一线架构师先进经验分享) IT架构设计研究组 编著   ISBN 978-7-121-22605-2 2014年4月出版 定价:49.00元 208页 ...

  5. fir.im Weekly - Stanford 的 Swift 课程来了

    上周提过,Swift 的 Github 主页上已经有了 >>「Port to Android」,这周重点推荐一下 Stanford 的 Swift 课程. Developing iOS 9 ...

  6. salesforce 零基础学习(十六)Validation Rules & Date/time

    上一篇介绍的内容为Formula,其中的Date/time部分未指出,此篇主要介绍Date/time部分以及Validation rules. 本篇参考PDF: Date/time:https://r ...

  7. 使用System.Drawing.Imaging.dll进行图片的合并

    在最近开发项目的时候有时候需要进行图片的合并,即将两张图片合并成功一张图片 合并图片的代码: #region 两张图片的合并 /// <summary > /// 将Image对象转化成二 ...

  8. jQuery插件之ajaxFileUpload

    原文:http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html ajaxFileUpload是一个异步上传文件的jQuery插件 ...

  9. 【原创】.NET读写Excel工具Spire.Xls使用(2)Excel文件的控制

                  本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html .NET读写Excel工具Spire.Xls使用文章 ...

  10. 一套后台管理html模版

    最近自己需要一套后台管理的模版,然后去网上查找,模版的确很多,但是适合我的并不多.我需要的模版是不会很大,我能够控制代码,样式不要太古朴,最好有点CSS3的效果.最后终于找到一张主页,然后再根据这个主 ...