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 ...
随机推荐
- Cocos2d-html5 笔记4: 粒子
今天看了cocos2d-html5里面的粒子系统相关的代码,首先看了代码中引用的两篇文章, 这两篇文章google上都可以搜到pdf的. The Ocean Spray in Your Face [j ...
- B. Pasha and String
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 从源码的角度解析View的事件分发
有好多朋友问过我各种问题,比如:onTouch和onTouchEvent有什么区别,又该如何使用?为什么给ListView引入了一个滑动菜单的功能,ListView就不能滚动了?为什么图片轮播器里的图 ...
- Android 常用 adb 命令
查看原文:http://blog.csdn.net/u010818425/article/details/52266593 (一)基础操作 安装app adb install -r xxx.apk / ...
- 重构4-Push Down Method(方法下移)
我们介绍了将方法迁移到基类以供多个子类使用的上移方法重构,今天我们来看看相反的操作.重构前的代码如下: public abstract class Animal { public void Bark( ...
- 转:android 录制视频的Demo
转:http://blog.csdn.net/peijiangping1989/article/details/7049991 在这里给出自己的一个测试DEMO,里面注释很详细.简单的视频录制功能. ...
- 基于Qt的Tcp协议的流程图
TCP(Transmission Control Protocol传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议.在qt中,Tcp协议主要是用QTcpServer和QTcpSock ...
- hdu1251
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- JDK 自带工具试用(一)
简述: 运维监控会用到JDK的小工具 说明: 1. jps 用来查看当前运行的Java进程 我在eclipse中起了一个web 应用 或者用jps -l 可以查看的更清楚一点 jps -v 看到103 ...
- javascript面向对象的理解(一)
第一次在园子发文: 关于js面向对象的理解: 工厂方式是什么?构造函数是什么?原形链?对象的引用? 1.对象是什么? 在js接触的比较多的就是对象了,比如: var arr = []; arr.num ...