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 ...
随机推荐
- matlab里的nargin
nargin是用来判断输入变量个数的函数,这样就可以针对不同的情况执行不同的功能.
- 将数组写入plist文件
data 加载plist [NSBundle mainBundle] [arr writeToURL:<#(NSURL *)#> atomically:<#(BOOL)#>]
- The server instance Witness rejected configure request; read its error log file for more information. The reason 1427, and state 31, can be of use for
数据库服务器做了镜像之后,发现有错误信息 The server instance Witness rejected configure request; read its error log file ...
- mysql编码设置 [http://blog.knowsky.com/254652.htm]
MYSQL 2009-09-11 15:37 阅读73 评论1 字号: 大大 中中 小小mysql> SHOW VARIABLES LIKE 'character_set_%';+------- ...
- Chapter 1 First Sight——30
The girl sitting there giggled. I'd noticed that his eyes were black — coal black. 那个坐在那里的女孩笑着.我注意到她 ...
- 第四十二节,configparser特定格式的ini配置文件模块
configparser用于处理特定格式的文件,其本质上是利用open来操作文件. 特定格式的ini配置文件模块,用于处理ini配置文件,注意:这个ini配置文件,只是ini文件名称的文本文件,不是后 ...
- gerrit push配置
第一种方法: 在home目录下,编辑.gitconfig文件,添加 [remote "review"] url = ssh://someone@oneip:port/project ...
- hdu_2842_Chinese Rings(矩阵快速幂)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题意:解开第k个环需要先解开前(k-2)个环,并留有第(k-1)环.问解开n环最少需要几步. 题 ...
- wps制作目录
01 设置标题为.标题1.标题2,标题302 引用--插入目录03 配置文档结构图.查看--可快速回到目录
- redis第一篇--综述
1 redis里边有数据库的概念.可分为1-255这些表.在存储或者查找的时候要指明. redis_sentinel 集群里边封装成了namespace这样的概念.与db是不一样的.