每天一道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 ...
随机推荐
- 译 - EF 6秘诀(第二版) - 目录
本博文系Entity Framework 6 Recipes, 2nd Edition的目录译文.保留原文,方便参考. 第一章 EF入门Chapter 1. Getting Started with ...
- Microchip 125 kHz RFID System Design Guide
Passive RFID Basics - AN680 INTRODUCTION Radio Frequency Identification (RFID) systems use radio fre ...
- Freescale OSBDM JM60仿真器
OSBDM-JM60 - 9S08JM60 Based OSBDM — It includes interfaces and firmware applied to all the targets s ...
- WatchKit Learning Resources
查看原文:http://leancodingnow.com/watch-kit-learning-resources/ WatchKit是Apple发布的用来开发Apple Watch应用的框架,本 ...
- javascript删除数组里的对象
Array.prototype.del = function(value) { //删除数组中指定的元素,返回新数组 function hasValue(array, value) { for(var ...
- jquery禁用右键、文本选择功能、复制按键的实现
同时适合IE.firefox.谷歌浏览器下适用,经过筛选代码如下 //禁用右键.文本选择功能.复制按键 $(document).bind(“contextmenu”,function(){return ...
- easy Html5 - Jquery Mobile之ToolBars(Header and Footer)
jquery 在web js框架上的风暴还在继续却也随着移动终端走向了mobile:那么jquery mobile到底包括些什么呢 简介工具栏是在移动网站和应用中的头部,尾部或者内容中的工具条:Jqu ...
- BZOJ 1497: [NOI2006]最大获利 最小割
1497: [NOI2006]最大获利 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1497 Description 新的技术正冲击着手 ...
- MyBatis学习练习
转自:http://ccchhhlll1988-163-com.iteye.com/blog/1415621 基本目的:利用Mybatis完成对一个表简单的select.insert.update.d ...
- ThreadLocal小记
API: public class ThreadLocal<T> extends Object 该类提供了线程局部 (thread-local) 变量. 这些变量不同于它们的普通相应物.由 ...