Easy 389题 Find the Difference
2016-09-14 23:42
369 查看
Question:Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
public class Solution {
public char findTheDifference(String s, String t) {
// char result=s.charAt(0); //there is a possibility that s is null and t has only one element
int i;
String str=s+t;
char result=str.charAt(0);
for(i=1;i<=str.length()-1;i++)
{
result^=str.charAt(i);
}
return result;
}
}
It's almost same as "single number". We use ^ again
exclusive OR operation(异或)。。。
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
public class Solution {
public char findTheDifference(String s, String t) {
// char result=s.charAt(0); //there is a possibility that s is null and t has only one element
int i;
String str=s+t;
char result=str.charAt(0);
for(i=1;i<=str.length()-1;i++)
{
result^=str.charAt(i);
}
return result;
}
}
It's almost same as "single number". We use ^ again
exclusive OR operation(异或)。。。
相关文章推荐
- Nodejs缓冲区
- JavaScript 数组详解
- JavaScript中的style.width/height和offsetWidth/height区别
- 【VS开发】Caffelib中出现的问题:强制链接静态库所有符号(包括未被使用的)
- [Jquery]Cannot read property 'msie' of undefined
- CSS3选择器的研究
- html js 定制进度条 (一)
- 《精通javascript》里的一些函数(二)
- JS-验证码倒计时
- 洛谷 P1118 [USACO06FEB] 数字三角形Backward Digit Su…
- CSS3新技能学习笔记
- JavaScript对象基础讲解
- react从入门到深入的教程、工具
- Bootstrap+AngularJS对话框实例
- div里ul li浮动之后父容器高度不能自动增加的解决方法
- JavaScript中创建对象的方法:工厂模式,构造函数模式, 原型模式
- css浮动float
- HTML小结1-编码
- javascript_basic_04之节点、元素
- JS-鼠标长按触发事件