博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode题解(1048):最长字符串链(Python)
阅读量:1901 次
发布时间:2019-04-26

本文共 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

转载地址:http://bczcf.baihongyu.com/

你可能感兴趣的文章
Visual Assist X的安装路径问题
查看>>
终端异常退出后,后台进程不关闭的解决办法
查看>>
Linux系统忘记root密码
查看>>
Linuxshell脚本在windows下编辑后执行出错
查看>>
硬链接不能跨分区的错误
查看>>
关于窗口Qt线程停止的问题
查看>>
centos NTP服务器配置总结
查看>>
QT 容器类之关联存储容器
查看>>
windows虚拟机搭建Qt开发环境之IOS
查看>>
Redhat安装Mplayer问题汇总
查看>>
查看linux是32位还是64位
查看>>
ffmpeg
查看>>
XCode编译器介绍
查看>>
X86汇编语言从实模式到保护模式14:用户程序编程接口及其实现
查看>>
SystemC自带example的simple_perf研习
查看>>
SystemC自带example的rsa研习
查看>>
Python实用小技巧
查看>>
美科学家研发BIC-TCP协议 速度是DSL六千倍
查看>>
AIDL使用注意
查看>>
SDL以及扩展库的交叉编译过程简介
查看>>