题目来源:
https://leetcode.com/problems/unique-email-addresses/
自我感觉难度/真实难度:easy/easy
题意:
分析:
熟悉使用string的相关操作即可
自己的代码:
class Solution(object): def numUniqueEmails(self, emails): """ :type emails: List[str] :rtype: int """ res=[] for string in emails: A=string.split('@') B=A[0].split('+') B[0]=B[0].replace('.','') res.append('"'+B[0]+'@'+A[1]+'"') resl=list(set(res)) resul=' and '.join(resl) print(resul) return len(resl)
代码效率/结果:
Runtime: 60 ms, faster than 58.66% of Python online submissions for Unique Email Addresses.
优秀代码:
class Solution(object): def numUniqueEmails(self, emails): """ :type emails: List[str] :rtype: int """ seen = set() for email in emails: local,domain = email.split('@') if '+' in local: local = local[:local.index('+')] seen.add(local.replace('.','')+'@'+domain) return len(seen)
代码效率/结果:
自己优化后的代码:
反思改进策略:
1.a,b=string.split('@',1) #切好的五花肉,可以分别放进两个不同的盒子里
2.去除list中重复元素,可以 list(set( A))
3.集合添加元素使用add