Longest Word in Dictionary through Deleting
来源:互联网 发布:23周胎儿四维彩超数据 编辑:程序博客网 时间:2024/04/29 22:36
leetcode第524题,竞赛题,给定一个字符串列表和一个单独的字符串,通过删除这个单独的字符串中某些字符可以在列表中找到与之相同的字符串。
乍一看无从下手,但是其实直接做还是挺简单的,首先按照题目要求,要把列表按照字典序排个序,这个python中有操作并不难,然后利用双指针法来匹配。匹配过程为,最外层循环遍历整个字典,取出字典中的每个单词,内层循环为单独字符串的指针,依次遍历每个字符,最里面判断是否与字典中取出的单词匹配,匹配上则移动指针。最后判断一下指针是不是已经到达了字典取出单词的末尾即可。
class Solution(object): def findLongestWord(self, s, d): """ :type s: str :type d: List[str] :rtype: str """ d.sort(key=lambda x: (-len(x), x)) for word in d: i = 0 for ch in s: if i < len(word) and word[i] == ch: i += 1 if i == len(word): return word return ""
0 0
- Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- leetcode524~Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- [LeetCode] Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- 524. Longest Word in Dictionary through Deleting
- Leetcode-524. Longest Word in Dictionary through Deleting
- 在本地用命令行创建一个git仓库,并推送到远程
- 自组织神经网络,适合新手阅读
- web实现跑马灯
- map--C++ STL 学习
- JavaSE_32th_容器(类集框架)
- Longest Word in Dictionary through Deleting
- 一个多项式a+b*x+c*x^2+d*x^3+...,输入该多项式的系数和x的值后打印出这个多项式的值。
- 自定义控件:等比例显示控件RatioLayout
- Lenovo ThinkStation P710
- 取消jQuery validate验证
- 面向对象编程思想---OOP
- Spring面试题及答案(69道)
- LeetCode 15 3Sum
- 端口扫描工具python-nmap的使用