Leecode刷题之旅-C语言/python-389 找不同
/*
* @lc app=leetcode.cn id=389 lang=c
*
* [389] 找不同
*
* https://leetcode-cn.com/problems/find-the-difference/description/
*
* algorithms
* Easy (54.68%)
* Total Accepted: 7.1K
* Total Submissions: 12.9K
* Testcase Example: '"abcd"\n"abcde"'
*
* 给定两个字符串 s 和 t,它们只包含小写字母。
*
* 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
*
* 请找出在 t 中被添加的字母。
*
*
*
* 示例:
*
* 输入:
* s = "abcd"
* t = "abcde"
* 输出:
* e
*
* 解释:
* 'e' 是那个被添加的字母。
*
*
*/
char findTheDifference(char* s, char* t) {
int len=strlen(s); int result=(int)t[];
for(int i=;i<len;i++){
result^=s[i];
result^=t[i+];
}
return (char)result;
}
相同的数异或为0,而俩数组只有一个字母不同,所以只要全都异或一遍就只剩下那个不同的字母的ascuii码,ascuii码是int类型,只要强制类型转换为char就行了。
#
# @lc app=leetcode.cn id=389 lang=python3
#
# [389] 找不同
#
# https://leetcode-cn.com/problems/find-the-difference/description/
#
# algorithms
# Easy (54.68%)
# Total Accepted: 7.1K
# Total Submissions: 12.9K
# Testcase Example: '"abcd"\n"abcde"'
#
# 给定两个字符串 s 和 t,它们只包含小写字母。
#
# 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
#
# 请找出在 t 中被添加的字母。
#
#
#
# 示例:
#
# 输入:
# s = "abcd"
# t = "abcde"
#
# 输出:
# e
#
# 解释:
# 'e' 是那个被添加的字母。
#
#
#
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
ch=0
for c in s+t:
ch^=ord(c)
return chr(ch)
Leecode刷题之旅-C语言/python-389 找不同的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
- Leecode刷题之旅-C语言/python-349两个数组的交集
/* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...
随机推荐
- Visual Studio强行修改运行平台和注意事项
默认情况下,会发现项目属性中只有一个Any CPU可供选择,无法修改运行平台. 解决方法如下: 右键“解决方案”,选择“属性”,此时发现每一个项目的平台依然只有Any CPU,点击右上角“配置管理器” ...
- (MUA)mutt-"No authenticators available" 发送不出去邮件
问题: 使用mutt发送邮件的时候,在调试模式下总是显示"No authenticators available", 后来在google上查到可能要设置验证方法gssapi,但是设 ...
- 如何理解springaop
初看aop,上来就是一大堆术语,而且还有个拉风的名字,面向切面编程,都说是oop的一种有益补充等等,一下子让你不知所措,心想着:怪不得很多人都和我说aop多难多难.当我看进去以后,我才发现:它就是一些 ...
- zabbix监控 linux/windows 主机tcp连接状态
更新内容:1).增加了对windows主机的tcp连接状态的监控2).修改linux主机的监控配置,使linux与windwos主机能够使用相同的模板tcp的连接状态对于监控服务器,尤其是Web服务器 ...
- 沉淀,再出发:sublime中快捷键和html标签的使用和生成以及使用markdown
沉淀,再出发:sublime中快捷键和html标签的使用和生成以及使用markdown 一.前言 工欲善其事,必先利其器.在软件代码的编写中,一定要知道IDE或者编辑器的快捷键的使用,这样可以提高很多 ...
- [COGS 2064]爬山
2064. 爬山 ★☆ 输入文件:mountain.in 输出文件:mountain.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 球有一天走在街上. 一个健 ...
- solr集群搭建(4.10.2)
配置环境为centos6.8 下载tomcat程序到服务器,并解压启动服务测试 [root@hadoop01 ~]# wget http://apache.claz.org/tomcat/tomcat ...
- 020.2.1 system
内容:System的常用方法1.currentTimeMillis()方法(与1970年相差多少毫秒),2.Properties getProperties():获取系统属性3.Set<Str ...
- 浅谈 JavaScript 中的继承模式
最近在读一本设计模式的书,书中的开头部分就讲了一下 JavaScript 中的继承,阅读之后写下了这篇博客作为笔记.毕竟好记性不如烂笔头. JavaScript 是一门面向对象的语言,但是 ES6 之 ...
- JAVA Color类
Color类用于定义颜色,java.awt.Color中提供了13个预定义的常量用来表示13中标准颜色,分别是: public static final Color white白色. public s ...