博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode: Python]389. Find the Difference
阅读量:3557 次
发布时间:2019-05-20

本文共 812 字,大约阅读时间需要 2 分钟。

题目:

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:eExplanation:'e' is the letter that was added.

方法一:性能46ms

class Solution(object):    def findTheDifference(self, s, t):        """        :type s: str        :type t: str        :rtype: str        """        res = ord(t[-1])        for i in range(len(s)):            res += ord(t[i]) - ord(s[i])        return chr(res)

方法二:性能39ms

class Solution(object):    def findTheDifference(self, s, t):        """        :type s: str        :type t: str        :rtype: str        """        a=ord(t[0])        for i in s+t[1:]:            a=a^ord(i)        return chr(a)

转载地址:http://cgcrj.baihongyu.com/

你可能感兴趣的文章
javaweb 学习笔记 session与cookie会话技术
查看>>
javaweb 一个Servlet 处理多个请求(运用反射机制)
查看>>
Servlet 实现文件上传
查看>>
Spring 基于xml装配bean和使用注解实现依赖注入
查看>>
Java 实现代理模式以及通过Spring AOP 实现代理模式
查看>>
AspectJ 通知类型及其实现
查看>>
Spring JdbcTemplate 配置
查看>>
mysql 事务管理
查看>>
Daily life -------- Autumn's Coming
查看>>
计算机网络 划分子网并搭建网络将子网进行连通
查看>>
Daily Life ---------------------1024
查看>>
PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分)
查看>>
计算机网络 静态路由配置练习(使用Cisco Packet Tracer进行配置)
查看>>
Java 多线程/并发 Synchronized学习笔记
查看>>
Java 并发 volatile学习笔记
查看>>
PAT (Advanced Level) Practice 1084-1087 题解
查看>>
PAT (Advanced Level) Practice 1033 To Fill or Not to Fill(贪心)
查看>>
PAT (Advanced Level) Practice 1114 Family Property (25 分)
查看>>
PAT (Advanced Level) Practice 1143 Lowest Common Ancestor
查看>>
2019年PAT甲级冬季考试总结
查看>>