Leetcode389
Find the Difference
Given two strings s and t which consist of only lowercase letters.
给出两个字符串,s和t,都是只有小写字母组成的。
String t is generated by random shuffling string s and then add one more letter at a random position.
字符串t是由字符串s其中在随机的位置添加一个字符组成的。
Find the letter that was added in t.
找出在t中增加的字符
Example:
Input:
s = "abcd"
t = "abcde" Output:
e Explanation:
'e' is the letter that was added. 我一开始的想法就是把每个字符加起来,然后连个字符串相差的字符对应的数,就是对应的不同的字符了,很难说明白就直接看代码好了。
char c = 0;
for (int i=0;i<t.length();i++)
{
c += t.charAt(i);
}
for (int i=0;i<s.length();i++)
{
c -= s.charAt(i);
}
return c 之后看了讨论区,发现有一个异或好方法,但是无论怎么想都没想通。只能先记下了。
public char findTheDifference(String s, String t) {
char c = 0;
for (int i = 0; i < s.length(); ++i) {
c ^= s.charAt(i);
}
for (int i = 0; i < t.length(); ++i) {
c ^= t.charAt(i);
}
return c;
}
Leetcode389的更多相关文章
- 每天一道LeetCode--389. Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- [Swift]LeetCode389. 找不同 | Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- 【leetcode389】389. Find the Difference
异或 找不同 —.— public class Solution { public char findTheDifference(String s, String t) { char temp = 0 ...
随机推荐
- 酷比魔方 iwork10 旗舰版
平板电脑自带的win10的一键恢复不是一般的慢,简直慢出个性了,历经5个小时...必须是个有耐心的人才敢一键恢复,不然强制关机就变砖了...
- go语法
背景 go语言算是比较常用的开发语言了,但是我发现自己在写代码的时候仍无法做到熟练掌握语法的程度,这个博客是我在因为语法不熟练而必须停下来的地方,整理下来方便查阅和记忆. 数组 ]int //arra ...
- sipcal
sudo apt-get install sipcalc sipcalc 10.120.148.xx/20
- iOS学习笔记(02) - 关键字 __kindof
1.__kindof:表示当前类或它的子类. 2.__kindof书写格式:放在类型前面,表示修饰这个类型. 3.__kindof优点:在调用的时候,很清楚的知道返回类型. 直接举一个例子来形容这个问 ...
- html5实现滚动文字
<div class="custom-notice"> <i class="icon-notice"></i> <ma ...
- HDU 2722 Here We Go(relians) Again (spfa)
Here We Go(relians) Again Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/ ...
- innerhtml 和value值有什么区别
value 值写在标签里面的,innerHTML写在<button type="button" onclick="myFunction()">Try ...
- 规划(纪念我在ACM道路上的一年)
现在已经是晚上一点了,我早早的躺在床上,不能入睡,因为睡觉前看了一下我们学校今年区域赛的成绩总结,派出八次队伍,七个铜-- 再加上这两天ACM迎新杯的筹备过程的问题,让我产生了深深的思考-- 去年司老 ...
- iOS应用程序内存查看工具
我要找的是一个可以检查应用程序中哪一个数组存贮的什么内容的工具. 网上搜到的工具名称是Allocations Instrument,后来一试发现不是我想要的.这还是一个后期调试阶段的内存检查工具. h ...
- 多项目中SVN权限管理精辟解析
本节和大家讨论一下多项目SVN权限管理,主要包括建立版本库,修改版本库配置文件,配置允许访问的用户,设置用户访问权限.下面我们就来看一下SVN权限管理.svn权限管理svn的权限管理涉及到一下文件:p ...