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的更多相关文章

  1. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  2. 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 ...

  3. [转载]Difference between <context:annotation-config> vs <context:component-scan>

    在国外看到详细的说明一篇,非常浅显透彻.转给国内的筒子们:-) 原文标题: Spring中的<context:annotation-config>与<context:componen ...

  4. 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 ...

  5. difference between forward and sendredirect

    Difference between SendRedirect and forward is one of classical interview questions asked during jav ...

  6. 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 ...

  7. MySQL: @variable vs. variable. Whats the difference?

    MySQL: @variable vs. variable. Whats the difference?   up vote351down votefavorite 121 In another qu ...

  8. 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 ...

  9. 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 ...

  10. js-FCC算法-Symmetric Difference

    创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组. 给出两个集合 (如集合 A = {1, 2, 3} 和集合 B = {2 ...

随机推荐

  1. UIStoryboard

    UIStoryboard 目录 概述 Storyboard的创建 Storyboard中的页面跳转 文件内跳转 文件外跳转 Segues 不同类型的视图控制器在UIStoryboard上的实现 概述 ...

  2. XCODE4.6从零开始添加视图

    转自:http://www.cnblogs.com/luoxs/archive/2012/09/23/2698995.html 对于很多初学者来说,肯定希望自己尝试不用傻瓜的“Single View ...

  3. 《嵌入式Linux基础教程》补充阅读建议电子数目下载

    第二章 <Linux内核设计与实现(原书第三版)> <深入理解Linux内核(第三版)> <深入理解Linux虚拟内存管理> 其他与Linux相关的电子书下载地址: ...

  4. Post Robot

    Problem Description DT is a big fan of digital products. He writes posts about technological product ...

  5. 【Android 界面效果41】Matrix 与 ColorMatrix

    Matrix: 简单用法就是直接使用它的setXX()方法 而高级一点来理解他就是去理解一个线性矩形 首先我们来认识线性矩形:(用画图粗略地画不要见怪) 分析: 那还有一组 MRERSP_0 MRER ...

  6. 《MFC游戏开发》笔记七 游戏特效的实现(一):背景滚动

    本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9344721 作者:七十一雾央 新浪微博:http:// ...

  7. 以NameValueCollection 修改URL中的查询参数

    以NameValueCollection 修改URL中的查询参数 本文参考于:http://www.c-sharpcorner.com/Blogs/9421/add-remove-or-modify- ...

  8. 关于ios项目沙盒中的文件和Xcode项目创建的文件

    //1.1获取在Xcode项目打开的情况下创建的Plist文件 NSString *path = [[NSBundle mainBundle]pathForResource:@"Profes ...

  9. JAXL发送房间消息

    使用composer形式安装的JAXL <?php require_once "vendor/autoload.php"; $client = new JAXL(array( ...

  10. freeCodeCamp:Diff Two Arrays

    比较两个数组,然后返回一个新数组,该数组的元素为两个给定数组中所有独有的数组元素.换言之,返回两个数组的差异. function diff(arr1, arr2) { var newArr = []; ...