LeetCode题解(0804):摩斯密码不同的单词(Python)
发布日期:2021-06-29 19:54:21
浏览次数:2
分类:技术文章
本文共 1089 字,大约阅读时间需要 3 分钟。
题目:(简单)
标签:字符串
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( N × k ) O(N×k) O(N×k) : k为单词平均长度 | O ( N ) O(N) O(N) | 44ms (78.68%) |
Ans 2 (Python) | O ( N × k ) O(N×k) O(N×k) : k为单词平均长度 | O ( N ) O(N) O(N) | 36ms (95.83%) |
Ans 3 (Python) |
解法一(暴力解法):
def uniqueMorseRepresentations(self, words: List[str]) -> int: morse = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."] codes = set() for word in words: code = "" for c in word.lower(): code += morse[ord(c) - 97] codes.add(code) return len(codes)
解法二(暴力解法的又一种形式):
def uniqueMorseRepresentations(self, words: List[str]) -> int: morse = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."] codes = set() for word in words: codes.add("".join([morse[ord(c) - 97] for c in word.lower()])) return len(codes)
转载地址:https://dataartist.blog.csdn.net/article/details/107076066 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月08日 14时33分30秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
SQL Server中Id自增列的最大Id是多少
2019-04-30
AdminLTE
2019-04-30
asp.net core Authorization
2019-04-30
sqlserver dateadd&datediff使用
2019-04-30
SonarQube
2019-04-30
Dapper.Contrib
2019-04-30
asp.ent core swagger
2019-04-30
asp.net core ajax post controller
2019-04-30
Nexus+Nginx+docker
2019-04-30
DTO
2019-04-30
DDD
2019-04-30
wcf messageEncoding="Mtom"
2019-04-30
asp.net core webapi post
2019-04-30
C#中使用TransactionScope类(分布式事务) 和 锁
2019-04-30
SecurityProtocolType
2019-04-30
Consul、Ocelot、Docker
2019-04-30
消息队列
2019-04-30
BPM/OA/审批流/工作流
2019-04-30