1.题目大意

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.

Example:

Input:
s = "abcd"
t = "abcde" Output:
e Explanation:
'e' is the letter that was added.

给定两个字符串,有其中一个字符串有一个多出来的字符,找出那个字符。这题的Example依然是给的不大好,下面再给一个example:

Input:
s = "adbc"
t = "bacde" Output:
e

2.思路

这题思路比较简单,下面提供几个思路,第一个是我的原始思路,利用字母实际上是基于ASCII代码,可以转换为十进制数字来计算的。

class Solution
{
public:
char findTheDifference(string s, string t)
{
int sum=0;
char ch;
if(s.size()>t.size()) swap(t,s); //s is the small one
for(int i=0; i<s.size(); i++)
{
sum-=(s[i]);
sum+=(t[i]);
}
sum+=(t[s.size()]);
ch=(char)(sum);
return ch;
}
};

 第二个思路是这篇文章里的Hash表的思路,但我个人感觉过于复杂,事实上这个思路的runtime表现也很不好。

第三个思路依然是上篇文章里的,基本想法是位运算思路,这个思路runtime跟第一个思路表现差不多,都是可以考虑的思路。

LeetCode - 389. Find the Difference - 三种不同解法 - ( C++ ) - 解题报告的更多相关文章

  1. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  2. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  3. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  4. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  5. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  6. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  7. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. LeetCode 647. Palindromic Substrings的三种解法

    转载地址 https://www.cnblogs.com/AlvinZH/p/8527668.html#_label5 题目详情 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同 ...

  9. LeetCode 389. Find the Difference (找到不同)

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

随机推荐

  1. Linux 常用命令整理

    系统 切换用户 su 关机/重新启动 shoutdown,reboot,halt,poweroff 内存数据写入磁盘 sync 查询命令用法  "命令 –help" 或 " ...

  2. 【Oracle】Oracle安装配置、创建数据库实例及用户和连接

    https://blog.csdn.net/wudiyong22/article/details/78904361 参考资料:https://www.cnblogs.com/hoobey/p/6010 ...

  3. iOS之动态计算文字的高度

    + (CGSize)boundingALLRectWithSize:(NSString *)txt Font:(UIFont *)font Size:(CGSize)size { NSMutableA ...

  4. 三层架构,Struts2,SpringMVC实现原理图

    三层架构,Struts2,SpringMVC实现原理图 三层架构实现原理 Struts2实现原理 SpringMVC实现原理

  5. keepalived+haproxy 安装配置

    1.安装配置keepalived 修改配置文件/etc/keepalived/keepalived.conf ! Configuration File for keepalived global_de ...

  6. js中回调函数写法

    第一种方式 function studyEnglish(who){ document.write(who+"学习英语</br>"); } function study( ...

  7. form表单的一个页面多个上传按钮实例

    /* * 图片上传 */ @RequestMapping("/uploadFile") @ResponseBody public String uploadFile(@Reques ...

  8. 位域 (Bit field)

    最近开始看编程之美这本书,里面有一道关于中国象棋将帅位置的简单问题,如下图所示,写一个程序输出将.帅的合法位置. 分析与解法 问题的本身并不复杂,只要把所有A.B 互相排斥的条件列举出来就可以完成本题 ...

  9. jetson tx2 安装tensorflow

    官方的安装方法很简单: 可能会出现以下错误: 说的是dist-packages 权限不允许 consider using the "--user" to option or che ...

  10. 009---linux进程管理

    进程管理 top 查看运行状态:top 查看cpu核心数:top and 1 查看cpu占用率最大:top and P free 查看内存状态:free 以M为单位:free -m 以G为单位:fre ...