LeetCode 524. Longest Word in Dictionary through Deleting
来源:互联网 发布:淘宝网购买流程 编辑:程序博客网 时间:2024/05/22 05:14
题目:
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
Example 1:
Input:
s = “abpcplea”, d = [“ale”,”apple”,”monkey”,”plea”]
Output:
“apple”
Example 2:
Input:
s = “abpcplea”, d = [“a”,”b”,”c”]
Output:
“a”
Note:
1. All the strings in the input will only contain lower-case letters.
2. The size of the dictionary won’t exceed 1,000.
3. The length of all the strings in the input won’t exceed 1,000.
思路:
如果对s去年一些字符,能够与对于vector d中的字符串相等,则它为 longest string in the dictionary ,如果有多个长度相等的,选择字典序最小的返回。(所有字符都小写,s和d[i]中的长度都小于1000)
先计算d的长度和计算s的长度,对vector d外循环,每次外循环计算一次d[i]的长度,j为对d[i]中的第j个索引,初始值为0,对k内循环(跳出循环条件是j和k都到末尾),不认结果怎么样,k都加1,j只有在s[k]等于d[i][j]时才加1。如果j和lenstr相等,即遍历了d[i]中string的所有位置,即这个是longest string,如果这个lenstr大于原先result的长度,直接替换;如果长度相等,如果d[i]的字典序小,则替换。
代码:
class Solution {public: string findLongestWord(string s, vector<string>& d) { string result; int lend=d.size();//计算d的长度 int lens=s.length();//计算s的长度 for(int i=0;i<lend;++i){//对vector d外循环 int lenstr=d[i].length();//每次外循环计算一次d[i]的长度 int j=0;//j为对d[i]中的第j个索引 for(int k=0;j<lenstr&&k<lens;++k){//对k内循环(跳出循环条件是j和k都到末尾),不认结果怎么样,k都加1 if(s[k]==d[i][j]){//只有在s[k]等于d[i][j]时,j才加1 ++j; } } if(j==lenstr){//如果j和lenstr相等,即遍历了d[i]中string的所有位置,即这个是longest string if(lenstr>result.length()){//如果这个lenstr大于原先result的长度,直接替换 result.replace(result.begin(),result.end(),d[i]); } if(lenstr==result.length()&&d[i]<result){//如果长度相等,如果d[i]的字典序小,则替换 result.replace(result.begin(),result.end(),d[i]); } } } return result; }};
输出结果: 106ms
- [LeetCode] Longest Word in Dictionary through Deleting
- Leetcode-524. Longest Word in Dictionary through Deleting
- Leetcode 524. Longest Word in Dictionary through Deleting
- LeetCode: 524. Longest Word in Dictionary through Deleting
- LeetCode 524. Longest Word in Dictionary through Deleting
- [LeetCode]524. Longest Word in Dictionary through Deleting
- LeetCode笔记:524. Longest Word in Dictionary through Deleting
- LeetCode 524. Longest Word in Dictionary through Deleting
- leetcode 524. Longest Word in Dictionary through Deleting
- leetcode 524. Longest Word in Dictionary through Deleting
- [Leetcode] 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
- 524. Longest Word in Dictionary through Deleting
- webview的使用
- 在maven中添加依赖,把自己写的插件
- linux基础入门学习
- 核密度估计(KDE)
- 100T核心数据库升级历险记
- LeetCode 524. Longest Word in Dictionary through Deleting
- 深入分析Java中的length和length()
- 如何跟开发就测试范围进行沟通?
- 判断素数及质因数分解
- 【转载】MATLAB函数句柄和匿名函数
- 44-z最小子数组
- Eclipse与MyEclipse的区别
- HBase数据迁移至Hive
- php调用云片网接口发送短信实例