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是用来判断输入变量个数的函数,这样就可以针对不同的情况执行不同的功能.
- 对方网络ping不通
后台接口往往部署在其他服务器上如果ping不同 很大可能是因为对方开防火墙的原因 解决方法:控制面板-windows防火墙-打开或关闭windows防火墙
- SVN通过域名连不上服务器地址(svn: E175002: OPTIONS request failed on '/svn/yx-SVN-Server' Connection refused: connect)
用域名直连就连不上,如果换成了ip直连就可以连接上去了 https://yx-server01/svn/yx-SVN-Server 换为了 https://192.168.188.208/svn/yx ...
- hdu_5791_Two(DP)
题目链接:hdu_5791_Two 题意: 给你两串数列,问你相同的子序列有多少个,要注意,可以重复,比如1 和1 1 1 ,相同的子序列为3个 题解: 就和求最长公共子序列差不多,只不过要全部加起来 ...
- HDU2206:IP的计算
Problem Description 在网络课程上,我学到了很多有关IP的知识.IP全称叫网际协议,有时我们又用IP来指代我们的IP网络地址,现在IPV4下用一个32位无符号整数来表示,一般用点分方 ...
- 第13章 Swing程序设计----JFrame窗体
JFrame窗体是一个容器,它是Swing程序中各个组件的载体,可以将JFrame看作是承载这些Swing组件的容器. 在开发应用程序时可以通过继承java.swing.JFrame类创建一个窗体,在 ...
- TCP/IP,http,socket,长连接,短连接——小结。
来源:http://blog.chinaunix.net/uid-9622484-id-3392992.html TCP/IP是什么? TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层. ...
- 图片应该放在drawable-hdpi下不要放在drawable下
图片应该放在drawable-hdpi下或者mipmap-hdpi 不要放在drawable下,要不然显示有些不同
- mysql 常用命令集锦[绝对精华]
一.连接MYSQL. 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL. 首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u roo ...
- C#抓取页面时候,获取页面跳转后的地址
static string fanhuiurl(string cahxunurl) { string url = ""; HttpWebRequest req = (HttpWeb ...