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 ...
随机推荐
- 剑指 offer set 3 旋转数组的最小数字
总结 1. 没有重复元素的旋转数组可用 logn 时间内求出结果. 解法有两个步骤, 先是求出发生旋转的点(以 array[0] 为支点求得), 然后用正常的二分查找给出结果 2. 有重复元素元素的旋 ...
- Socket 之 同步以及异步通信
用netstat侦听下端口状态 同步通信: 预定义结构体,同步通信没有多线程异步委托回调,所以无需预定义结构体 客户端Client: class Program { static void Main( ...
- SQL Server三种表连接原理
在SQL Server数据库中,查询优化器在处理表连接时,通常会使用一下三种连接方式: 嵌套循环连接(Nested Loop Join) 合并连接 (Merge Join) Hash连接 (Hash ...
- Dispatcher中Invoke与BeginInvoke
[同步]Invoke Application.Current.Dispatcher.Invoke(AutoIncreaseNumber); [异步]BeginInvoke Application.Cu ...
- linux-i386(ubuntu)下编译安装gsoap_2.8.17过程记录
过程记录 : 1.下载gsoap_2.8.17.zip 并 解压 : $unzip gsoap_2.8.17.zip 2.进入解压后的目录gsoap-2.8 3.自动配置编译环境: $ ...
- 【Java/Android性能优2】Android性能调优工具TraceView介绍
本文参考:http://www.trinea.cn/android/android-traceview/ Android自带的TraceView堪比java的性能调优工具visualvm线程视图,可以 ...
- XML DTD验证
XML DTD验证 一.什么是DTD 文档类型定义(DTD:Document Type Definition)可定义合法的XML文档构建模块.它使用一系列合法的元素来定义文档的结构. DTD 可被成行 ...
- oracle数据操纵语言(DML)data manipulation language
数据库操纵语言(DML)用于查询和操纵模式对象中的数据,它不隐式地提交当前事务. SELECTINSERTUPDATEDELETECALLEXPLAIN PLANLOCK TABLEMERGE使用算术 ...
- 《UNIX环境高级编程》学习心得 二
窝萌来看我们看到这本书里的第一个程序 #include "apue.h" #include <dirent.h> int main(int argc, char *ar ...
- MVC 菜鸟学习记录2
自定义Html.Helper扩展方法 在Models文件夹下新建一个静态类,取名为:MyHtmlHelper.cs 将MyHtmlHelper设置为static,并写入以下的一个静态方法 public ...