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的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. iOS 7 UI Transition – Porting View Controller Layouts from iOS 6

    http://www.mobinett.com/2013/08/19/ios7-ui-transition-porting-view-controller-layouts-ios6/

  2. 树莓派安装RASPBIAN系统

    买了个树莓派3B,安装Raspbian时出现问题,能ping通,但是无法建立ssh链接!对于我这种只有网线和电源的玩家来说打击太大. 找了一下原因,如下链接所述: http://downloads.r ...

  3. Eclipse不能自动编译 java文件

      在网上的解决方法 方法参考如下: (1) Window-->Preferences-->General-->Workspace  有个"Build automatica ...

  4. 关于Mysql查询带单引号及插入带单引号字符串问题

    1.转为带参数查询 String sql=""select id from student where name='?'; Connection connect = DriverM ...

  5. 新浪微博UWP UI意见征求

    各位园主,卑职最近在忙一些新浪微博UWP的事儿,其中有一些UI上的design和实现,拿出来见见公婆,请大家给个意见: 您是喜欢A还是B.麻烦直接回在评论区了,写A或B,愿意多写几句意见的更欢迎! 先 ...

  6. java提高篇(二九)-----Vector

    在java提高篇(二一)-–ArrayList.java提高篇(二二)-LinkedList,详细讲解了ArrayList.linkedList的原理和实现过程,对于List接口这里还介绍一个它的实现 ...

  7. java.rmi.NoSuchObjectException: no such object in table

    jmx链接的时候,最简单的例子都行不通,郁闷,出现了: 参考:http://reiz6153.blog.163.com/blog/static/401089152009442723208/ 代码: M ...

  8. 利用定时器实时显示<input type="range"/>的值

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. js笔记——js异常处理

    异常捕获 try...catch结构: try { //需要捕获的代码块 } catch (e) { console.log(e.name + ": " + e.message); ...

  10. SSM 三大框架整合

    上一篇已经讲了整个各个子模块的创建过程以及它们之间的依存关系, 那么这一篇就来正式的整合三大框架(SSM)了. 1, 准备环境1.1 为每个War包工程创建一个Server 那么 添加了Server后 ...