LeetCode题解(1048):最长字符串链(Python)
发布日期:2021-06-29 20:09:42
浏览次数:2
分类:技术文章
本文共 998 字,大约阅读时间需要 3 分钟。
题目:(中等)
标签:哈希表、动态规划
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O ( W l o g W + W × L 2 l o g L ) O(WlogW+W×L^2logL) O(WlogW+W×L2logL) : 其中L为单词长度 | O ( W ) O(W) O(W) | 308ms (31.70%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一(哈希表):
class Solution: def longestStrChain(self, words: List[str]) -> int: words.sort(key=lambda x: len(x)) ans = 1 count = { } for word in words: size = len(word) if size == 1: count[word] = 1 else: min_from = float("inf") for i in range(size): sorted_word = "".join(sorted(word[:i] + word[i + 1:])) if sorted_word in count: min_from = min(min_from, count[sorted_word]) if min_from != float("inf"): ans = max(ans, size - min_from + 1) count["".join(sorted(word))] = min_from else: count["".join(sorted(word))] = size return ans
转载地址:https://dataartist.blog.csdn.net/article/details/109844778 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月09日 01时55分14秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Taro中如何使用cssModules
2019-04-30
taro中别名引入路径
2019-04-30
taro中hook的使用
2019-04-30
使用element-ui的table组件,使用三元表达式筛选数据
2019-04-30
vue打包项目报错Failed to load resource: the server responded with a status of 500 (Internal Server Error)
2019-04-30
解决小程序在高层级页面滑动时,低层级页面会跟着滑动的问题
2019-04-30
子组件watch监听父组件数据,从而判断是否重新加载数据
2019-04-30
小程序码生成和解析问题
2019-04-30
小程序修改回答功能实现
2019-04-30
vue中 指令与函数的结合使用
2019-04-30
微信小程序更新授权接口wx.getUserProfile后,个别用户出现无法登录的问题
2019-04-30
微信小程序canvas绘制圆角矩形
2019-04-30
微信小程序canvas裁剪图片中间部分
2019-04-30
my docker tutorial
2019-04-30
gdb debug summary
2019-04-30
python learning
2019-04-30
collections
2019-04-30
shallow copy & deep copy
2019-04-30
unit test & mock
2019-04-30
exception
2019-04-30