【LeetCode】929. Unique Email Addresses 解题报告(Python)
作者: 负雪明烛
 id: fuxuemingzhu
 个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/unique-email-addresses/description/
题目描述
Every email consists of a local name and a domain name, separated by the @ sign.
For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name.
Besides lowercase letters, these emails may contain '.'s or '+'s.
If you add periods ('.') between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address. (Note that this rule does not apply for domain names.)
If you add a plus ('+') in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example m.y+name@email.com will be forwarded to my@email.com. (Again, this rule does not apply for domain names.)
It is possible to use both of these rules at the same time.
Given a list of emails, we send one email to each address in the list. How many different addresses actually receive mails?
Example 1:
Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails
Note:
- 1 <= emails[i].length <= 100
 - 1 <= emails.length <= 100
 - Each emails[i] contains exactly one ‘@’ character.
 
题目大意
判断有多少个不重复的email.
邮箱的规则是这样的:域名部分不用管,只管用户名部分,用户名中如果包含有'.',那么把这个'.'给去掉;如果用户名中有'+',那么把加号以及后面的用户名部分全部去掉。
解题方法
set + 字符串操作
Python处理这种字符串题目简直不要太简单,这个题是周赛第一题,用了6分钟读完题目,然后提交通过了。
首先把'.'给替换掉,然后查找是不是包含'+',如果有的话,只保留其前面的部分就好了。
因为要去重,所以要使用一个set保存所有不重复的结果。
时间复杂度是O(N),空间复杂度是O(N)。
class Solution(object):
    def numUniqueEmails(self, emails):
        """
        :type emails: List[str]
        :rtype: int
        """
        eset = set()
        for email in emails:
            simper = self.simpifyEmail(email)
            eset.add(simper)
        return len(eset)
    def simpifyEmail(self, email):
        local, domain = email.split("@")
        local = local.replace('.', '')
        plus_i = local.find('+')
        if plus_i != -1:
            local = local[:plus_i]
        return local + "@" + domain
代码可以优化变短,使用replace代替find函数。
class Solution:
    def numUniqueEmails(self, emails):
        """
        :type emails: List[str]
        :rtype: int
        """
        res = set()
        for email in emails:
            name, domain = email.split("@")
            name = name.split("+")[0].replace(".", "")
            res.add(name + "@" + domain)
        return len(res)
参考资料
日期
2018 年 10 月 28 日 —— 啊,悲伤的周赛
【LeetCode】929. Unique Email Addresses 解题报告(Python)的更多相关文章
- LeetCode 929 Unique Email Addresses 解题报告
		
题目要求 Every email consists of a local name and a domain name, separated by the @ sign. For example, i ...
 - [leetcode] 929. Unique Email Addresses (easy)
		
统计有几种邮箱地址. 邮箱名类型:local@domain 规则:1. local中出现"."的,忽略. a.bc=abc 2. local中出现"+"的,+以 ...
 - [LeetCode] 929. Unique Email Addresses 唯一的电邮地址
		
Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...
 - LeetCode 929.Unique Email Addresses
		
Description Every email consists of a local name and a domain name, separated by the @ sign. For exa ...
 - [LeetCode] 929. Unique Email Addresses 独特的邮件地址
		
Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...
 - LeetCode 929. Unique Email Addresses (独特的电子邮件地址)
		
题目标签:String 题目说明 有两个规则针对于 local name. 所以先把local name 和 domain name 分开. 两个规则是: rule 1:'.' 会被去除. (利用re ...
 - 929. Unique Email Addresses
		
929. Unique Email Addresses Easy 22766FavoriteShare Every email consists of a local name and a domai ...
 - 【Leetcode_easy】929. Unique Email Addresses
		
problem 929. Unique Email Addresses solution: class Solution { public: int numUniqueEmails(vector< ...
 - 【leetcode】929. Unique Email Addresses
		
题目如下: Every email consists of a local name and a domain name, separated by the @ sign. For example, ...
 
随机推荐
- No.2 R语言在生物信息中的应用—模式匹配
			
目的: 1. 计算自定义模序在所有蛋白质的匹配位点和次数 2. 输出超过阈值的蛋白质序列到Hit_sequences.fasta 3. Hit_sequences.fasta中序列用小写字母,匹配用大 ...
 - Bebug与Release版本
			
如果调试过程无调试信息,检查编译选项是否切换到了release下 比如Cfree5等编译器 ms为了方便调试才诞生了DEBUG版. 这也导致了MFC有两个功能一至但版本不同的类库,一个为DEBUG版, ...
 - kubernetes部署kube-controller-manager服务
			
本文档介绍部署高可用 kube-controller-manager 集群的步骤. 该集群包含 3 个节点,启动后将通过竞争选举机制产生一个 leader 节点,其它节点为阻塞状态.当 leader ...
 - C语言中的位段----解析
			
有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位. 例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可. 为了节省存储空间并使处理简便,C语言又提供了一种数据结 ...
 - 利用抖音Cookie充值接口提取支付链接,个人调起原生微信h5支付宝h5支付
			
最近开始搞一些个人支付通道的开发,方便个人不用和第三方平台签约就能收款,省去很多流程手续的成本. 然后翻了一下网上并没有太多现成的技术教程,只能自己研究着搞了. 这次要分享的是利用抖音的充值接口,去分 ...
 - Portrait Photography Beginners Guide
			
Please visit photoandtips稻糠亩 for more information. 六级/考研单词: vogue, derive, gorgeous, thereby, strict ...
 - day03 部署NFS服务
			
day03 部署NFS服务 NFS的原理 1.什么是NFS 共享网络文件存储服务器 2.NFS的原理 1.用户访问NFS客户端,将请求转化为函数 2.NFS通过TCP/IP连接服务端 3.NFS服务端 ...
 - TCP中的TIME_WAIT状态
			
TIME_WAIT的存在有两大理由 1.可靠地实现TCP全双工连接的终止 2.允许老的可重复分节在网络中消失. 对于理由1,我们知道TCP结束需要四次挥手,若最后一次的客户端的挥手ACK丢失(假设是客 ...
 - Linux定时任务crontable简介
			
Linux下定时执行任务的方法:Linux之crond 服务介绍:https://www.cnblogs.com/liang-io/p/9596294.html http://www.mamicode ...
 - 最新的Android Sdk 使用Ant多渠道批量打包
			
实例工程.所需的文件都在最后的附件中. 今天花费了几个小时,参考网上的资料,期间遇到了好几个问题, 终于实现了使用Ant批量多渠道打包,现在,梳理一下思路,总结使用Ant批量多渠道打包的方法:1 ...