LeetCode - 389. Find the Difference - 三种不同解法 - ( C++ ) - 解题报告
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++ ) - 解题报告的更多相关文章
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 647. Palindromic Substrings的三种解法
转载地址 https://www.cnblogs.com/AlvinZH/p/8527668.html#_label5 题目详情 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同 ...
- LeetCode 389. Find the Difference (找到不同)
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
随机推荐
- Oracle 缓存命中率问题一则(里面有个问题咨询大佬们)
近日,核心数据库频繁抱出数据库缓存命中率过低,于是开始进行排查. 1.监控软件告警信息 2.抓取告警时间段内的awr报告进行分析 3.execute与parse命中率过低,说明分析(硬解析与软解析)的 ...
- Undefined symbols for architecture arm64: "_OBJC_CLASS_$XXX", referenced from: objc-class-ref in XXX
ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 ...
- 奇(qi)谋(ji)巧(yin)计(qiao)
一.打表法 0.http://oeis.org/ 1.差分序列:https://blog.csdn.net/wu_tongtong/article/details/79115921 对于一个多项式产生 ...
- ASP.NET MVC4.0 后台获取不大前台传来的file
<td>选择图片</td> <td> <input type="file" id="uploadImg" name=& ...
- 安装 vue-devtools
1. github下载 vue-devtools: git clone https://github.com/vuejs/vue-devtools 2. node install 安装包 3. vi ...
- php的基础知识(四)
14.数组: 索引数组: 下标就是数字开始的. $arr = ['a','b','c',1,2,3]; 关联数组: $arr = [ 'a' => 'b', 'c' => 'd'; 'e' ...
- yii学习笔记(7),数据库操作,联表查询
在实际开发中,联表查询是很常见的,yii提供联表查询的方式 关系型数据表:一对一关系,一对多关系 实例: 文章表和文章分类表 一个文章对应一个分类 一个分类可以对应多个文章 文章表:article 文 ...
- django基本配置项-5
目录 BASE_DIR 项目路径 DEBUG 模式 ROOT_URLCONF DATABASES 数据库设置 国际化 静态文件 访问静态文件示例 ALLOWED_HOSTS TEMPLATES 模板设 ...
- react--基本用法
1.安装了babel 但是终端执行 babel src --out-dir build命令时说"babel:command is not found" 经百度,找到solution ...
- 滑雪_KEY
滑雪 ( skiing.pas/c/cpp) [题目描述] MM 参加一个滑雪比赛,滑雪场是一个 N×M 的矩形, MM 要从起点( 1, 1)滑到( N,M).矩形中每个单位格子有一个海拔高度值 h ...