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. C#中AES加密和解密

    /// AES加密 /// </summary> /// <param name="inputdata">输入的数据</param> /// & ...

  2. struts2 result type的类型

    一共十种类型 1.dispatcher 默认的类型,相当于servlet的foward,服务器端跳转.客户端看到的是struts2中配置的地址,而不是真正页面的地址.一般用于跳转到jsp页面 2.re ...

  3. JPA 系列教程18-自动把firstName+lastName合并为name字段

    需求 设计的国际化网站,页面需要输入firstName,lastName,后台数据库只需要存储name属性. 页面获取的firstName,lastName持久化到数据库name属性,规则按照,分隔保 ...

  4. 将数据动态加载到Echarts饼图中

    需求描述 Echarts中的官方示例是将数据的设定写好在页面的配置项中的,但在实际的开发展示中,我们需要按照需求通过调用后台的接口获取数据,再将数据加载到特定的Echarts饼图中. 实现效果 实现步 ...

  5. php 分页类(2)

    <?phpinclude("connection.php");$perNumber=10; //每页显示的记录数$page=$_GET['page']; //获得当前的页面值 ...

  6. PHP常用函数之数组篇

    分类:数组分为索引数组和关联数组.索引数组既是指的我们的数组下表为阿拉伯数字,关联数组则是非阿拉伯数字. 定义: 5.4版本之前 $arr = array('name' => '张三', 'ag ...

  7. 【读书笔记】Linux源码注释

    第二章 大概的内部组成 IO端口寻址: 统一寻址: 就是把地址归入存储器寻址范围. 独立寻址: 跟存储器分开,专门的寻址空间 没怎么理解, PC机一般都是采用独立寻址, 见下图 在linux里,可以在 ...

  8. Windows常用的监视数据指标

  9. tableview cell添加3D动画

    当cell显示之前,会先调用该方法,因此给cell添加动画,在这个方法里面即可. -(void)tableView:(UITableView *)tableView willDisplayCell:( ...

  10. Chapter 2 Open Book——15

    The rest of the week was uneventful. I got used to the routine of my classes. 这周剩下的时间都是平淡无事的.我就是正常的上 ...