问题描述:

给定两个字符串 st,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

示例:

输入:
s = "abcd"
t = "abcde" 输出:
e 解释:
'e' 是那个被添加的字母。

方法1:

 class Solution(object):
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
for i in t:
if i not in s:
return i
elif s.count(i) != t.count(i):
return i

amazing:

 class Solution(object):
def findTheDifference(self, s, t):
"""
:type s: str
:type t: str
:rtype: str
"""
num_s = 0
num_t = 0
for i in s:
num_s += ord(i)
for i in t:
num_t += ord(i)
return chr(num_t -num_s)

2018-09-29 06:58:05

LeetCode--389--找不同的更多相关文章

  1. Java实现 LeetCode 389 找不同

    389. 找不同 给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例: 输入: s = " ...

  2. LeetCode 389——找不同

    1. 题目 2. 解答 2.1. 方法一 将 s 和 t 转化为 Python 的列表,然后遍历列表 s 的元素,将它们从列表 t 中删除,最后列表 t 中会余下一个元素,即为所求. class So ...

  3. 【LeetCode】389.找不同

    389.找不同 知识点:哈希表.抵消思想: 题目描述 给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. ...

  4. 力扣(LeetCode)389. 找不同

    给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例: 输入: s = "abcd&quo ...

  5. LeetCode 513. 找树左下角的值(Find Bottom Left Tree Value)

    513. 找树左下角的值 513. Find Bottom Left Tree Value 题目描述 给定一个二叉树,在树的最后一行找到最左边的值. LeetCode513. Find Bottom ...

  6. LeetCode.1002-寻找共有字符(Find Common Characters)

    这是悦乐书的第375次更新,第402篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第236题(顺位题号是1002).给定仅由小写字母组成的字符串A,返回列表中所有字符串都 ...

  7. 9. leetcode 389. Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  8. 位运算 leecode.389. 找不同

    //给定两个字符串 s 和 t,它们只包含小写字母. //字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. //请找出在 t 中被添加的字母 char findTheDifferenc ...

  9. LeetCode 389 Find the Difference 解题报告

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

  10. leetcode ex3 找出穿过最多点的直线 Max Points on a Line

    题目 https://oj.leetcode.com/problems/max-points-on-a-line/ 答案与分析 http://www.aiweibang.com/yuedu/18326 ...

随机推荐

  1. selenium css定位方式

  2. CentOS7下Docker中构建Jenkins容器

    背景 在CentOS搭建Docker完成后,我们需要在Docker中搭建Jenkins用来实现工程自动部署. 安装前提 jdk已安装,安装目录如:usr/java/jdk1.8.0_161 maven ...

  3. Git冲突与解决方法【转】

    本文转载自:https://www.cnblogs.com/gavincoder/p/9071959.html Git冲突与解决方法 1.git冲突的场景 情景一:多个分支代码合并到一个分支时: 情景 ...

  4. 颠倒的价牌|2013年蓝桥杯A组题解析第四题-fishers

    颠倒的价牌 小李的店里专卖其它店中下架的样品电视机,可称为:样品电视专卖店. 其标价都是4位数字(即千元不等). 小李为了标价清晰.方便,使用了预制的类似数码管的标价签,只要用颜色笔涂数字就可以了(参 ...

  5. P2234 [HNOI2002]营业额统计(Splay树)题解

    思路:Splay数查找前驱后继 代码: #include<iostream> #include<cstdio> #include<cstring> #include ...

  6. 【做题】Codeforces Round #453 (Div. 1) D. Weighting a Tree——拆环

    前言:结论题似乎是我的硬伤…… 题意是给你一个无向图,已知连接到每一个点的边的权值和(为整数,且属于区间[-n,n]),需要求出每条边权值的一个合法解(都要是在区间[-2*n^2,2*n^2]内的整数 ...

  7. FAQ Flyway

    https://flywaydb.org/documentation/faq What is the best strategy for dealing with hot fixes? You hav ...

  8. 【开机自启】Linux下设置MySql自动启动

    1.将服务文件拷贝到init.d下,并重命名为mysql cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld 2.赋予可 ...

  9. 题解——Codeforces Round #508 (Div. 2) T3 (贪心)

    贪心的选取最优解 然后相减好 记得要开long long #include <cstdio> #include <algorithm> #include <cstring ...

  10. 题解——洛谷P2734 游戏A Game 题解(区间DP)

    题面 题目背景 有如下一个双人游戏:N(2 <= N <= 100)个正整数的序列放在一个游戏平台上,游戏由玩家1开始,两人轮流从序列的任意一端取一个数,取数后该数字被去掉并累加到本玩家的 ...