LeetCode题解(1002):查找常用字符(Python)
发布日期:2021-06-29 19:55:07
浏览次数:3
分类:技术文章
本文共 876 字,大约阅读时间需要 2 分钟。
题目:(简单)
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( N × K ) O(N×K) O(N×K) : K为字符串长度 | O ( K ) O(K) O(K) | 56ms (76.09%) |
Ans 2 (Python) | O ( N × K ) O(N×K) O(N×K) : K为字符串长度 | O ( K ) O(K) O(K) | 56ms (76.09%) |
Ans 3 (Python) |
LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。
解法一(哈希表):
def commonChars(self, A: List[str]) -> List[str]: hashmap = { } for c in A[0]: if c not in hashmap: hashmap[c] = 1 else: hashmap[c] += 1 for a in A[1:]: for k in hashmap: hashmap[k] = min(hashmap[k], a.count(k)) ans = [] for k, v in hashmap.items(): while v > 0: ans.append(k) v -= 1 return ans
解法二(使用collections.Counter类):
def commonChars(self, A: List[str]) -> List[str]: ans = collections.Counter(A[0]) for a in A[1:]: ans &= collections.Counter(a) return list(ans.elements())
转载地址:https://dataartist.blog.csdn.net/article/details/107098083 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月25日 04时39分12秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
WCF传递Stream时,同时传递其它参数的问题
2019-04-30
abp vNex
2019-04-30
关闭页面前js提示
2019-04-30
window.open关闭后刷新父页面
2019-04-30
ajax异步提交修改按钮文字
2019-04-30
draw.io
2019-04-30
Convert VB.NET to C#
2019-04-30
VB内部函数(三)——财务函数
2019-04-30
Financial.IPmt/Financial.PPmt
2019-04-30
ASP.NET Boilerplate
2019-04-30
服务器后端发展三个阶段
2019-04-30
easyui-快速布局
2019-04-30
EPPlus--ExcelPackage 导出Excel
2019-04-30
setInterval
2019-04-30
window.open关闭窗体后刷新
2019-04-30
easyui.dialog
2019-04-30
js保存指定html
2019-04-30
会改bug不值钱,预防bug才值钱
2019-04-30
confluence--团队协同与知识管理工具
2019-04-30