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 ...
随机推荐
- ural 1356. Something Easier(数论,哥德巴赫猜想)
1356. Something Easier Time limit: 1.0 secondMemory limit: 64 MB “How do physicists define prime num ...
- Time complexity of ArrayList in Java
The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add ope ...
- Valgrind: memcheck of memleak/mem-uninitialization; massif usage
first install valgrind, its newest ver is 3.11, and stops updating since 2015/12. in centos, yum ins ...
- sublime3安装
1.下载安装程序 http://c758482.r82.cf2.rackcdn.com/Sublime%20Text%20Build%203059%20Setup.exe 2.下载注册机 http:/ ...
- android性能调优之traceview的使用
1.在开始使用TraceView你要注意: 你的设备和模拟器必须设置SD card 和 你的程序拥有对SD card 具有读写操作的权限( <uses-permission android:na ...
- eclipse添加桌面快捷方式
linuxmint系统 输入sudogedit /usr/share/applications/eclipse.desktop,在打开的文件中输入以下参数: [Desktop Entry] Encod ...
- PHP运行模式(cgi,fast-cgi,cli, ISAPI ,web模块模式)【转载】
PHP运行模式有5钟: 1)cgi 通用网关接口(Common Gateway Interface))2)fast-cgi 常驻 (long-live) 型的 CGI3)cli 命令行运行 (C ...
- Struts2的运行机制简介
1.客户端通过URL请求tomcat 2.URL找到对应站点的WEB.xml 发现里面有 struts2配置 3.执行StrutsPrepareAndExecuteFilter类的init方法 4 ...
- C++随机崩溃捕捉处理
1. 会引起异常的几个原因(主要记录目前遇到过的几个问题) 程序读取了无效的内存地址 堆栈的溢出,比如无限循环导致那段内存溢出,比如把size为20的缓存拷贝到size为10的缓存块等 无法申请到有效 ...
- jQuery实现父窗口的问题
因为先前遇到的问题,所以我考虑采用 IFRAME 来隔离不同的脚本,从而实现我需要的效果. 在框架中,我用 JavaScript 获取 JSON 数据,组织成 HTML 代码,最后将其填充至上层文档的 ...