作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

https://leetcode.com/problems/find-the-difference/

  • Difficulty: Easy

题目描述

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.

题目大意

字符串t是字符串s打乱顺序之后,又随机添加了一个字符,求这个字符。

解题方法

方法一:字典统计次数

题目没有要求空间复杂度,因此可以用HashTable记录每个元素出现的次数,最后只出现了一次的就是那个被添加上去的元素。不提。

另外,可以用一个数组,把两个字符串中出现了的字符对应到数组当中,把s数组对应位置++,t对应位置–,出现了两次的元素则会抵消,否则,就是出现了一次的。

java解法如下:

public class Solution {
public char findTheDifference(String s, String t) {
int[] chars=new int[26];
for(int i=0; i<s.length(); i++){
chars[s.charAt(i) - 'a']++;
}
for(int i=0; i<t.length(); i++){
chars[t.charAt(i) - 'a']--;
}
for(int i=0; i<chars.length; i++){
if(chars[i]!=0){
return (char) ('a' + i);
}
}
return '0';
}
}

AC:9ms

python写法如下:

class Solution:
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
scount, tcount = collections.Counter(s), collections.Counter(t)
for t in tcount:
if tcount[t] > scount[t]:
return t

方法二:异或

想到了之前做的那个题,数组中其余元素出现了两次,找出数组中只出现了一次的那个数字。完完全全一样的题目。

方法是异或运算。

public class Solution {
public char findTheDifference(String s, String t) {
int answer = 0;
for(int i=0; i<s.length(); i++){
answer ^= s.charAt(i) - 'a';
}
for(int i=0; i<t.length(); i++){
answer ^= t.charAt(i) - 'a';
}
return (char) ('a' + answer);
}
}

AC:9ms

python写法如下:

class Solution(object):
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
return chr(reduce(lambda x, y : x ^ y, map(ord, s + t)))

方法三:排序

把字符串转成了列表,然后进行排序,从前向后遍历slist,如果tlist的该位置和slist不同,那么这个就是tlist添加出来的字符。如果遍历结束没有找到,那么tlist最后的字符就是答案。

class Solution:
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
slist, tlist = list(s), list(t)
slist.sort()
tlist.sort()
for i in range(len(slist)):
if slist[i] != tlist[i]:
return tlist[i]
return tlist[-1]

日期

2017 年 1 月 7 日
2018 年 11 月 10 日 —— 这么快就到双十一了??

【LeetCode】389. Find the Difference 解题报告(Java & Python)的更多相关文章

  1. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  2. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  3. LeetCode 389 Find the Difference 解题报告

    题目要求 Given two strings s and t which consist of only lowercase letters. String t is generated by ran ...

  4. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  5. 【LeetCode】283. Move Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...

  6. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  7. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  8. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  9. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  10. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

随机推荐

  1. mysql proxy 数据库读写分离字符集乱码

    mysql proxy 数据库读写分离字符集乱码 解决办法 在对应配置后端数据库服务器的配置.cnf中加入如下代码 init-connect='SET NAME UTF8' skip-characte ...

  2. markdown语法之如何使用LaTeX语法编写数学公式

    CSDN-markdown语法之如何使用LaTeX语法编写数学公式 目录 目录 正文 标记公式 行内公式 块级公式 上标和下标 分数表示 各种括号 根号表示 省略号 矢量表示 间隔空间 希腊字母 特殊 ...

  3. javaSE高级篇4 — 反射机制( 含类加载器 ) — 更新完毕

    反射机制 1.反射机制是什么?----英文单词是:reflect.在java.lang包下---这才是java最牛逼的技术 首先提前知道一句话----在java中,有了对象,于是有了类,那么有了类之后 ...

  4. 关于ai算法的一个点子

    长久以来,一直想要有自己的原生算法. 今天灵感图然来了: 想到, 一个事务不但要看它本身,也要看欣赏它的人. 要研究两个方面. 你要研究音乐,也要研究欣赏音乐的人. 人之所以会欣赏音乐,而牛不可以(对 ...

  5. 如何将List集合中相同属性的对象合并

    在实际的业务处理中,我们经常会碰到需要合并同一个集合内相同属性对象的情况,比如,同一个用户短时间内下的订单,我们需要将各个订单的金额合并成一个总金额.那么用lambda表达式和HashMap怎么分别处 ...

  6. centos 7 重新获取IP地址

    1.安装软件包 dhclient # yum install dhclient 2.释放现有IP # dhclient -r 3.重新获取 # dhclient 4.查看获取到到IP # ip a

  7. Linux学习 - IP地址配置

    1 首先选择桥接模式 2 配置IP.子网掩码.网关.DNS setup 本例中使用的是无线网连接, IP地址:  192.168.3.195 子网掩码:  255.255.255.0 网关: 192. ...

  8. zabbix之二进制安装

    #:参考官方网站 https://www.zabbix.com/documentation/4.0/manual/installation/install_from_packages/debian_u ...

  9. ExecutorService 线程池详解

    1.什么是ExecutorService,为什么要使用线程池? 许多服务器应用程序都面向处理来自某些远程来源的大量短小的任务,每当一个请求到达就创建一个新线程,然后在新线程中为请求服务,但是频繁创建新 ...

  10. Linux提取命令grep 有这一篇就够了

    grep作为linux中使用频率非常高的一个命令,和cut命令一样都是管道命令中的一员.并且其功能也是对一行数据进行分析,从分析的数据中取出我们想要的数据.也就是相当于一个检索的功能.当然了,grep ...