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 ...
随机推荐
- sketch 相关论文
sketch 相关论文 Sketch Simplification We present a novel technique to simplify sketch drawings based on ...
- ETCD分布式存储部署
一.ETCD 概述 ETCD 是一个分布式一致性k-v存储系统,可用于服务注册发现与共享配置.具有一下优点: 简单: 相比于晦涩难懂的paxos算法,etcd基于相对简单且易实现的raft算法实现一致 ...
- CentOS7的安装与配置
Linux系统以前接触的不多,主要是公司的网站部署在了一台安装了Ubuntu系统的机器上.是典型的LAMP架构的产物,因为偶而需要更新网站内容及需要定期备份.所以学习了一些Ubuntu & A ...
- 2.2 Oracle之DML的SQL语句之多表查询以及组函数
一.SQL的多表查询: 1.左连接和右连接(不重要一方加(+)) SELECT e.empno,e.ename,d.deptno,d.dname,d.loc FROM emp e,dept d WHE ...
- FFMS2 API 译文 [原创]
FFMS2 又称 FFmpegSource2,参阅 https://github.com/FFMS/ffms2. 原文:https://github.com/FFMS/ffms2/blob/maste ...
- PytorchZerotoAll学习笔记(一)
Pytorch的安装请参考torch的官方文档,传送门:https://pytorch.org/get-started/locally/ Numpy的复习 如果你之前没有学过Numpy的话,建议去看看 ...
- cmd下执行mysql
1. mysql -uroot -p1234; 2. show databases; 3. use testnode; 4. 创建数据库表 CREATE DATABASE `node` DEFA ...
- hwconfig命令详解
基础命令学习目录首页 转载自系统技术非业余研究 本文链接地址: hwconfig查看硬件信息 最近经常要测试新硬件,了解硬件的具体型号和参数就非常重要,过去经常透过lspci, dmidecode, ...
- easyui panel异步获取后台数据在前台显示
我在使用easyui的时候,想做一个向下图所示的效果,这个panel的样式已经做好了,想从后台异步获取json数据,然后填入到文本框中,不知道哪位大神能给点指导?万分感谢! 放入表单中,使用form对 ...
- HTML基础学习总结
一.HTML的一些基本描述 全称:Hyper Text Markup Language 定义:超文本标记语言,是标记语言而不是编程语言,使用标记标签来描述网页,所以也被称为网页 格式:标签对里面放纯文 ...