LeetCode题解(1178):猜字谜(Python)
发布日期:2021-06-29 20:09:46
浏览次数:3
分类:技术文章
本文共 1134 字,大约阅读时间需要 3 分钟。
题目:(困难)
标签:哈希表、位运算
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( W × L × l o g L + P × 2 7 × 7 × l o g 7 ) O(W×L×logL+P×2^7×7×log7) O(W×L×logL+P×27×7×log7) : 其中L为谜底长度 | O ( W × L + 2 7 × 7 ) O(W×L+2^7×7) O(W×L+27×7) | 916ms (35.29%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一:
class Solution: def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]: def all_chance(chars): lst = [chars[0]] chars = list(sorted(set(chars[1:]))) size = len(chars) res = [] def recursion(i): if i == size: res.append("".join(sorted(lst))) else: lst.append(chars[i]) recursion(i + 1) lst.pop() recursion(i + 1) recursion(0) return res count = collections.Counter() for word in words: count["".join(sorted(set(word)))] += 1 ans = [] for puzzle in puzzles: now = 0 for chance in all_chance(puzzle): now += count[chance] ans.append(now) return ans
转载地址:https://dataartist.blog.csdn.net/article/details/109848156 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月15日 22时24分18秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
深度学习word2vec笔记之基础篇
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
艾维利时间管理法
2019-04-30
我的sql自定义分页
2019-04-30
阿里入职培训,针不搓
2019-04-30
我的游标分页
2019-04-30
js比较两个String字符串找出不同,并将不同处高亮显示
2019-04-30
进程以下的那些事儿
2019-04-30
宏内核与微内核
2019-04-30
python异步函数中调用同步函数
2019-04-30
Python异步库里面的队列
2019-04-30