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 ...
随机推荐
- cookie机制和session机制的区别(面试题)
一.cookie机制和session机制的区别 具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态的方案. 同时我们也看到,由于才服务器端保持状态的 ...
- js对象,类
js闭包网站:http://www.cnblogs.com/qieguo/p/5457040.html 有权访问另一个函数作用域内变量的函数都是闭包. 这里 inc 函数访问了构造函数 a 里面的变量 ...
- touchesBegan: withEvent: <--- with UIScrollView / UIImageView
touchesBegan: withEvent: / touchesMoved: withEvent: / touchesEnded: withEvent: 等只能被UIView捕获(如有问题请指出对 ...
- C# 读取二进制文件
using UnityEngine; using System.Collections; using System; using System.IO; public class Test : Mono ...
- 转:jmeter之线程组
虽然有三个添加线程组的选项,名字不一样, 创建之后,其界面是完全一样的.之前的版本只有一个线程组的名字.现在多一个setUp theread Group 与terDown Thread Group 1 ...
- 协同过滤(CF)算法
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- hdu_5807_Keep In Touch(分段dp)
题目链接:hdu_5807_Keep In Touch 题意: 在Byteland一共有nn个城市,编号依次为11到nn,同时有mm条单向道路连接着这些城市,其中第ii条道路的起点为u_iui, ...
- H5之重力感应篇
手机的重力感应支持里,有两个主要的事件: 1. OrientationChange (在屏幕发生翻转的时候触发) 2. DeviceOrientation+DeviceMotion(重力感应与陀螺仪) ...
- Java 学习路线
java 入门到精通 转自:http://forum.hibernate.org.cn作者:robbinJava Learning Path (一).工具篇 一. JDK (Java Develop ...
- 【转】Ecshop 后台增加一个左侧列表菜单menu菜单的方法
cshop 后台增加一个左侧列表菜单menu菜单需要修改三个文件:/admin/includes/inc_menu.php/admin/includes/inc_priv.php/languages/ ...