LeetCode:Find the Difference_389
LeetCode:Find the Difference
【问题再现】
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.
【优质算法】
public char findTheDifference(String s, String t) {
char[] sArray = s.toCharArray();
char[] tArray = t.toCharArray();
char t1 = 0;
for(char c1:sArray)
t1^=c1;
for(char c2:tArray)
t1^=c2;
return(char)t1;
}
【题后反思】
对异或运算符^的理解:
异或运算符是用符号“^”表示的,其运算规律是:
两个操作数的位中,相同则结果为0,不同则结果为1 。
一个重要的运算性质:
A^B^B=A;
该题运用了异或的运算规律和性质,算法可以这么理解,将两条字符串异或在一起 ,即(0^a^b^c^d)^(a^b^c^d^e )其中相同的都异或为0了,剩下的就是那一个被添加的字符。
括号分别表示两条字符串,其中第二条比第一条多了一个e.
LeetCode:Find the Difference_389的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- 基于AutoCAD的ObjectARX之NET扩展(mcnetarx)-AcdbEntGet
1.AcdbEntGet用于获取实体的组码. 示例: ' 定义保存实体名称的变量 Dim ent() As Integer = New Integer() {} ' 获取最后一个实体 mcnetarx ...
- 第一次自己写jquery图片延迟加载插件,不通用,但修改一下还是可以使用到很多页面上的
不断修改完善中…… /*! * jquery.lazyoading.js *自定义的页面图片延迟加载插件,比网上的jquery.lazyload简单,也更适合自己的网站 *使用方法: 把img 的cl ...
- 影响Java EE性能的十大问题(转)
本文作者是一名有10多年经验的高级系统架构师,他的主要专业领域是Java EE.中间件和JVM技术.他在性能优化和提升方面也有很深刻的见解,下面他将和大家分享一下常见的10个影响Java EE性能问题 ...
- FreeRTOS任务栈
configTOTAL_HEAP_SIZE 定义堆大小,FreeRTOS 内核,用户动态内存申请,任务栈,任务创建,信号量创建,消息队列创建 等都需要用这个空间.
- 怎样让.bat文件开机自启动
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\R ...
- PHP学习第一天笔记——php的基本语法
1.php嵌入到html中的方式 (1) <?php.....?> 标准风格(推荐) (2)<script language="php">......< ...
- Replication的犄角旮旯(一)--变更订阅端表名的应用场景
<Replication的犄角旮旯>系列导读 Replication的犄角旮旯(一)--变更订阅端表名的应用场景 Replication的犄角旮旯(二)--寻找订阅端丢失的记录 Repli ...
- Python札记 -- MongoDB模糊查询
最近在使用MongoDB的时候,遇到了使用多个关键词进行模糊查询的场景.竹风使用的是mongoengine库. 查了各种资料,最后总结出比较好用的方法.先上代码,后面进行详细说明.如下: #!/usr ...
- 【Win10】UAP/UWP/通用 开发之 RelativePanel
[Some information relates to pre-released product which may be substantially modified before it's co ...
- mongodb(回滚)
事实上mongodb是不支持事务的,个人理解原因如下:1.避免大量对document加锁,从而影响性能,2.非关系型的数据库,从设计上就应能尽可能的比较关联复杂的多document,一个数据应能记录在 ...