每天一道LeetCode--389. Find the Difference
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)
每天一道LeetCode--389. Find the Difference的更多相关文章
- LeetCode 389. Find the Difference (找到不同)
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- LeetCode 389. Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- 9. leetcode 389. Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- LeetCode 389 Find the Difference 解题报告
题目要求 Given two strings s and t which consist of only lowercase letters. String t is generated by ran ...
- LeetCode - 389. Find the Difference - 三种不同解法 - ( C++ ) - 解题报告
1.题目大意 Given two strings s and t which consist of only lowercase letters. String t is generated by r ...
- LeetCode: 389 Find the Difference(easy)
题目: Given two strings s and t which consist of only lowercase letters. String t is generated by rand ...
- 【一天一道LeetCode】#219. Contains Duplicate II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【一天一道LeetCode】#121. Best Time to Buy and Sell Stock
# 一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say ...
- 【一天一道LeetCode】#303.Range Sum Query - Immutable
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
- 【一天一道Leetcode】#190.Reverse Bits
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
随机推荐
- 解决Unable to connect to a repository at URL 禁止访问 (forbidden)
连接SVN报如下错误. Unable to connect to a repository at URL 禁止访问 (forbidden) 1. 右键点击本地副本,TortoiseSV ...
- Don’t use Suspend and Resume, but don’t poll either.
http://www.paradicesoftware.com/blog/2014/02/dont-use-suspend-and-resume-but-dont-poll-either/ Don’t ...
- uva140 - Bandwidth
Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orderi ...
- uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...
- 用AJAX技术聚合RSS
有时候,你的Blog可能需要这样的功能: 在自己Blog上聚合并显示朋友Blog的最新文章,这样方便自己及时了解朋友的消息,另外,也方便访问者找到和本Blog相关的blog和文章. 这个功能你可以叫它 ...
- 一个小巧的C++Log输出到文件类 (转)
http://blog.csdn.net/dpsying/article/details/17122739 有时候需要输出一些程序运行的信息,供我们不需要调试就可以直接查看程序运行状态.所以我们需 ...
- Android开发 Unity3D基础 Android Development
开发环境 Window 7 Unity3D 3.3.0 MB525 defy Android 2.1-update1 本次学习: 1.认识Unity 2.Unity3D环境搭建与Android软件生成 ...
- 标准库 - fmt/print.go 解读
// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a B ...
- 在应用程序中实现对NandFlash的操作
以TC58NVG2S3ETA00 为例: 下面是它的一些物理参数: 图一 图二 图三 图四 图五 图6-0 图6-1 说明一下,在图6-1中中间的那个布局表可以看做是实际的NandFlash一页数据的 ...
- hive和hbase整合的原因和原理
为什么要进行hive和hbase的整合? hive是高延迟.结构化和面向分析的: hbase是低延迟.非结构化和面向编程的. Hive集成Hbase就是为了使用hbase的一些特性.或者说是中和它们的 ...