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的更多相关文章

  1. 每天一道LeetCode--389. Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  2. [Swift]LeetCode389. 找不同 | Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  3. 【leetcode389】389. Find the Difference

    异或 找不同 —.— public class Solution { public char findTheDifference(String s, String t) { char temp = 0 ...

随机推荐

  1. javascript动画效果之透明度(修改版)

    在编写多块同时触发运动的时候,发现一个BUG, timer = setInterval(show, 30);本来show是一个自定义函数,当设为timer = setInterval(show(one ...

  2. ( ̄▽ ̄") 没钱了

    ( ̄▽ ̄") 没钱了 TimeLimit: 1000ms  MenoryLimit:65536KB 64-bit integer IO format:%lld Problem Descrip ...

  3. android.telephony.SmsManager 短信笔记

    android 几种发送短信的方法 http://www.oschina.net/question/163910_27409 <uses-permission android:name=&quo ...

  4. VMWare网络链接三种方式

    本文转自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/03/15/1985084.html VMware虚拟机上网络连接(networ ...

  5. 移动UI

    UI设计需要关注:色彩,信息布局,交互流程,用户体验,尺寸等

  6. 升级3.2.3后 could not find driver

    求解,之前一切正常,升级3.2.3后提示找不到数据库驱动我用的是mysql,也没有设置DB_DSN错误位置FILE: C:\Users\Administrator\workspace\test\Thi ...

  7. 一个forward_list C++primer

    #include<iostream> #include<forward_list> using namespace std; int main() { forward_list ...

  8. HTML5的自定义属性data-*详细介绍和JS操作实例

    当然高级浏览器下可通过脚本进行定义和数据存取.在项目实践中非常有用. 例如: 复制代码 代码如下: <div id = "user" data-uid = "123 ...

  9. oracle添加sequence

    CREATE SEQUENCE seq_tm_function INCREMENT BY 1 -- 每次加几个 START WITH 100000015 -- 从1开始计数 NOMAXVALUE -- ...

  10. Ubuntu上安装mono

    How do I use badgerports? badgerports is an Ubuntu repository. In order to use it, you must add it t ...