Find the Difference
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.
我的思路: 利用哈希(桶排序?),开两个大小为26的int数组,利用下标表示二十六个字母,比较两者内容是否相同。
结果:可以通过,还需发现其他的可能性。
别的思路:
1. 位运算 by yuming.wei
class Solution {
public:
char findTheDifference(string s, string t) {
int ans = ;
for(int i = ; i < s.length(); ++i) ans ^= s[i]^t[i];
ans ^= t[t.length() - ];
return char(ans);
}
};
不用解释了,非常简单直接的方法。其实和之前做过的一道题很像,但是没想起来。
2.C++特性 by Ren.W
class Solution {
public:
char findTheDifference(string s, string t) {
char diff = ;
int cnt[] = {};
for (char c : s) cnt[c]++;
for (char c : t) if (--cnt[c] < ) return c;
return diff;
}
};
C++的一个新标准,有时间可以去学习一下。
3. 神似位运算的朴素巧解
char findTheDifference(char* s, char* t) {
int sum1=,sum2=;
for(;*s;s++)
sum1+=*s;
for(;*t;t++)
sum2+=*t;
return sum2-sum1;
}
Find the Difference的更多相关文章
- Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)
--reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...
- What's the difference between a stub and mock?
I believe the biggest distinction is that a stub you have already written with predetermined behavio ...
- [转载]Difference between <context:annotation-config> vs <context:component-scan>
在国外看到详细的说明一篇,非常浅显透彻.转给国内的筒子们:-) 原文标题: Spring中的<context:annotation-config>与<context:componen ...
- What's the difference between <b> and <strong>, <i> and <em> in HTML/XHTML? When should you use each?
ref:http://stackoverflow.com/questions/271743/whats-the-difference-between-b-and-strong-i-and-em The ...
- difference between forward and sendredirect
Difference between SendRedirect and forward is one of classical interview questions asked during jav ...
- Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...
- MySQL: @variable vs. variable. Whats the difference?
MySQL: @variable vs. variable. Whats the difference? up vote351down votefavorite 121 In another qu ...
- Distribute numbers to two “containers” and minimize their difference of sum
it can be solved by Dynamical Programming.Here are some useful link: Tutorial and Code: http://www.c ...
- difference between append and appendTo
if you need append some string to element and need set some attribute on these string at the same ti ...
- js-FCC算法-Symmetric Difference
创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组. 给出两个集合 (如集合 A = {1, 2, 3} 和集合 B = {2 ...
随机推荐
- KMP算法具体解释(转)
作者:July. 出处:http://blog.csdn.net/v_JULY_v/. 引记 此前一天,一位MS的朋友邀我一起去与他讨论高速排序,红黑树,字典树,B树.后缀树,包含KMP算法,只有在解 ...
- because it is not a variable 编译错误解决方案
1,将Stuct换为class 2,使用中间变量, 如Point p=new Point(x,y); this.Location=p; 而不能直接给struct赋值,因为值类型是不能改变的,必须生成新 ...
- 代码片段 - Golang 创建 .tar.gz 压缩包
Golang创建 .tar.gz 压缩包 tar 包实现了文件的打包功能,可以将多个文件或目录存储到单一的 .tar 文件中,tar 本身不具有压缩功能,只能打包文件或目录: import " ...
- Linux 测试网速
Linux 测试网速 1.直接wget -O /dev/null http://speedtest.wdc01.softlayer.com/downloads/test10.zip 一个10M的文件, ...
- IOS UIButton 自定义的补充学习
一直自定按钮 遇到两个做的不好的地方. 1 是按钮响应方法时候状态自定义不好看 按压感不明显 2 是button上的title 是我自己用label写上去的 而不是用button的属性 这两天终于运用 ...
- mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)
1:添加控制器类文件HomeController.cs其代码如下: using System; using System.Collections.Generic; using System.Linq; ...
- SOA资料
实施: 基于J2EE体系架构搭建符合SOA架构的运营管理平台 成功经验: 携程旅行网在SOA架构方面的探索 SOA在互联网系统中的应用
- Java、Android中Math详解
java.math.Math类常用的常量和方法: Math.PI 记录的圆周率 Math.E记录e的常量 Math.abs 求绝对值 Math.sin 正弦函数 Math.asin 反正弦函数 Mat ...
- 关于Django中的表单验证
ModelForm 和 普通的Form 都可以做表单验证 对于ModelForm如果只是想验证其中一部分model中的field,可以指定:内部类Meta的fields元素: fields = ('x ...
- TQ210开发板NFS挂载android4.0.4的rootfs的方法
首先声明的是,我使用的u-boot是自己移植的u-boot2013.01.01而非天嵌官方的那个,至于使用官方的u-boot如何去实现nfs挂载rootfs我没怎么研究过,不过原理方法都是一致的. 主 ...