Longest Word in Dictionary through Deleting - LeetCode
题目链接
Longest Word in Dictionary through Deleting - LeetCode
注意点
- 长度一样的字符串要按字典序返回较小的
解法
解法一:遍历字典中的单词,用一个变量i来记录单词中的某个字母的位置,我们遍历给定字符串,如果遍历到单词中的某个字母来,i自增1,如果没有,就继续往下遍历。这样如果最后i和单词长度相等,说明单词中的所有字母都按顺序出现在了字符串s中。如果能得到,而且单词长度大于等于结果ret的长度,我们再看是否需要更新结果ret,有两种情况是必须要更新结果ret的,一个是当前单词长度大于结果ret的长度,另一种是当前单词长度和ret相同,但是字母顺序小于结果ret,这两种情况下更新结果ret即可
class Solution {
public:
string findLongestWord(string s, vector<string>& d) {
string ret = "";
int i,m,n = d.size();
for(i = 0;i < n;i++)
{
int j = 0;
m = d[i].size();
for(char c:s)
{
if(j < m && c == d[i][j]) j++;
}
if(j == m)
{
if(m > ret.size()) ret = d[i];
else if(m == ret.size() && ret > d[i]) ret = d[i];
}
}
return ret;
}
};

小结
- 一开始没看懂题目是什么意思,理解成最长前缀了,其实题目是问能否将s中的某些字母删除而得到字典中的单词
Longest Word in Dictionary through Deleting - LeetCode的更多相关文章
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- [LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词
Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...
- #Leetcode# 524. Longest Word in Dictionary through Deleting
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...
- LeetCode——Longest Word in Dictionary through Deleting
1. Question Given a string and a string dictionary, find the longest string in the dictionary that c ...
- [Swift]LeetCode524. 通过删除字母匹配到字典里最长单词 | Longest Word in Dictionary through Deleting
Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...
- 524. Longest Word in Dictionary through Deleting【Medium】【删除后得到的字典中的最长单词】
Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...
- 524. Longest Word in Dictionary through Deleting
Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...
- leetcode 524. Longest Word in Dictionary through Deleting 通过删除字母匹配到字典里最长单词
一.题目大意 https://leetcode.cn/problems/longest-word-in-dictionary-through-deleting 给你一个字符串 s 和一个字符串数组 d ...
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
随机推荐
- [Lua] 尾调用消除(tail-call elimination)
<Lua程序设计(第2版)> 6.3 正确的尾调用(proper tail call) Lua是支持尾调用消除(tail-call elimination)的,如下面对函数g的调用就是尾调 ...
- 一句话打印'*'图案(列表推导式, 人生苦短, 我用Python)
```python # coding=utf-8 print ('\n'.join(['*'*6 for i in range(4)])) # ****** # ****** # ****** # * ...
- Jmeter实战
Jmeter实战 入门篇 1.下载与使用 下载地址:http://jmeter.apache.org/download_jmeter.cgi 开源,基于java编写,所以得有jdk(jre)环境,下载 ...
- Datawhale MySQL 训练营 Task6 实战项目
作业 项目十:行程和用户(难度:困难) Trips 表中存所有出租车的行程信息.每段行程有唯一键 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键.St ...
- jenkins的时间与服务器的时间不一致
解决办法:
- 【坚持】Selenium+Python学习记录 DAY10
2018/05/31-2018/06/1 [官方文档](https://www.jetbrains.com/help/pycharm/set-up-a-git-repository.html) 通过p ...
- Java那些事-泛型通配符
Java的类型通配符,可以出现在类.方法上面.最常用的方式就是集合类,例如List,Set等类上面. 通配符类型 有泛型参数 List 有无类型标识 List< ? > 有通用的标识 Li ...
- 【Coursera-ML-Notes】线性回归(上)
什么是机器学习 关于机器学习,有以下两种不同的定义. 机器学习是研究如何使电脑具备学习能力,而不用显式编程告诉它该怎么做. the field of study that gives computer ...
- chattr和lsattr命令详解
基础命令学习目录首页 原文链接:http://www.ha97.com/5172.html PS:有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chat ...
- iOS 源代码混淆(初步混淆)
我们可以用classdump对原程序进行 dump,像上篇文章( Class-dump 安装和使用记录(导出应用的头文件)),我们可以看到所有.h 文件全暴露了(如下图) 点击HWAccount.h后 ...