作者: 负雪明烛
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. 1 <= emails[i].length <= 100
  2. 1 <= emails.length <= 100
  3. 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)的更多相关文章

  1. LeetCode 929 Unique Email Addresses 解题报告

    题目要求 Every email consists of a local name and a domain name, separated by the @ sign. For example, i ...

  2. [leetcode] 929. Unique Email Addresses (easy)

    统计有几种邮箱地址. 邮箱名类型:local@domain 规则:1. local中出现"."的,忽略. a.bc=abc 2. local中出现"+"的,+以 ...

  3. [LeetCode] 929. Unique Email Addresses 唯一的电邮地址

    Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...

  4. LeetCode 929.Unique Email Addresses

    Description Every email consists of a local name and a domain name, separated by the @ sign. For exa ...

  5. [LeetCode] 929. Unique Email Addresses 独特的邮件地址

    Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...

  6. LeetCode 929. Unique Email Addresses (独特的电子邮件地址)

    题目标签:String 题目说明 有两个规则针对于 local name. 所以先把local name 和 domain name 分开. 两个规则是: rule 1:'.' 会被去除. (利用re ...

  7. 929. Unique Email Addresses

    929. Unique Email Addresses Easy 22766FavoriteShare Every email consists of a local name and a domai ...

  8. 【Leetcode_easy】929. Unique Email Addresses

    problem 929. Unique Email Addresses solution: class Solution { public: int numUniqueEmails(vector< ...

  9. 【leetcode】929. Unique Email Addresses

    题目如下: Every email consists of a local name and a domain name, separated by the @ sign. For example, ...

随机推荐

  1. R语言与医学统计图形-【26】ggplot2主题函数

    ggplot2绘图系统--主题函数 1. theme函数 theme_*系列函数提供了9种不同的风格. theme_grey/gray/bw/linedraw/light/minimal/classi ...

  2. Python异步IO之select

    1. select模块的基本使用(以socket为例) 1 # -*- coding:utf-8 -*- 2 # Author:Wong Du 3 4 import select 5 import s ...

  3. javaSE高级篇7 — 设计原则和设计模式 — 设计模式慢慢更( 这是思想层次篇 )

    1.什么是设计原则? 设计原则就是面向对象的原则嘛,即:OOP原则 换句话说:就是为了处理类与类之间的关系( 包括接口.类中的方法 ) 2.OOP设计原则有哪些? 1).开闭原则:就是指对拓展开放.对 ...

  4. 【STM32】基于正点原子『探索者』开发板的烧录

    项目需要一个功能,开发板范例正好有,就买了一块,不过还是有点贵 我手边没有J-Link 用的都是串口烧录 烧录时,先打开右上的开关 如果是仿真器烧录,它无法供电,需要接12V适配器或是杜邦线供电 然后 ...

  5. tomcat 之 session服务器 (memcache)

    #: 在tomcat各节点安装memcached [root@node1 ~]# yum install memcached -y #: 下载tomcat所需的jar包(此处在视频中找软件) [roo ...

  6. oracle(数据文件)

    --创建数据文件 create tablespace--创建表空间同时创建数据文件 create temporary tablespace --创建临时表空间的同时创建临时数据文件 alter tab ...

  7. 【Python】【Module】hashlib

    用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 import hashlib # ######## ...

  8. springMVC WebApplicationInitializer 替代web.xml 配置Servlet 之原理

    Servlet 3.0之前 ,xml  配置 在过去搭建spring + springMCV ,首先第一步要做的是什么 ,就是要配置web.xml 文件 ,把springMVC 中的Servlet 加 ...

  9. B树和B+树原理图文解析

    B树与B+树不同的地方在于插入是从底向上进行(当然查找与二叉树相同,都是从上往下) 二者都通常用于数据库和操作系统的文件系统中,非关系型数据库索引如mongoDB用的B树,大部分关系型数据库索引使用的 ...

  10. 『与善仁』Appium基础 — 20、Appium元素定位

    目录 1.by_id定位 2.by_name定位 3.by_class_name定位 4.by_xpath定位 5.by_accessibility_id定位 6.by_android_uiautom ...