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中按规定的字符截取字符串
CREATE OR REPLACE FUNCTION "F_SPLIT" (p_str IN CLOB, p_delimiter IN VARCHAR2) RETURN ty_st ...
- python基础 列表和字符串
1.列表和字符串最大区别就是 列表可以改变,字符串不可以改变 列表: welldone = [ "hutu","python","java" ...
- Cocopods Search失败的坑
最近看了下如何使用cocopods来制作自己的公有库,然后果断的按照教程做了一遍,然后提交审核.完成之后意外的发现使用pod search xxx的时候报了一大堆的日志出来,pod的其他功能可以使用, ...
- 环境配置之 Debug 和 Release - iOS
便于开发.打包中在不同环境(测试.生产)间属性的切换更加方便便捷流畅,故创建设置此方式方法,希望对大家能有所帮助. 首先,创建 Configurations Setting File(.xcconfi ...
- Vue--- 一点车项目 6个小时实际看了10天(完结)
一个项目 环境安装 使用了 cli 脚手架 Koa2 workpackage 其他小的不计 前端Vue组件搭建 数据的简单测试交互 数据库的设计 创建.连接接数据库 前台[表单/分类] ...
- .Net 上传文件到ftp服务器和下载文件
突然发现又很久没有写博客了,想起哎呦,还是写一篇博客记录一下吧,虽然自己还是那个渣渣猿. 最近在做上传文件的功能,上传到ftp文件服务器有利于管理上传文件. 前面的博客有写到layui如何上传文件,然 ...
- wordpress | 网站访问速度优化方案(Avada)
一.谷歌字体 原因: Wordpress系统默认使用谷歌字体,在国内谷歌域名被屏蔽,所以导致操作反应慢. 解决方法: 对于后台:找到Wordpress这个文件 /wp-includes/script- ...
- Python入门 —— 05时间日期处理小结
此文多涉及基础,如果想要深入理解则到文末,有提供链接 涉及对象 1. datetime 2. timestamp 3. time tuple 4. string 5. date - datetime基 ...
- canvas+javascript实现淘宝商品放大镜效果
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- See You Again——我最后的汇编程序
汇编语言:课程设计2 前言 由于本人水平不够,这里的课程设计2的程序实现并没有像王爽书中所说的那样可以不依赖于操作系统运行. 这里的程序依然要在dos下运行,而且没有实现引导现有操作系统的功能. 该程 ...