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 ...
随机推荐
- Caffe Blob针对图像数据在内存中的组织方式
Caffe使用Blob结构在CNN网络中存储.传递数据.对于批量2D图像数据,Blob的维度为 图像数量N × 通道数C × 图像高度H × 图像宽度W 显然,在此种场景下,Blob使用4维坐标定位数 ...
- Python3中isdigit(), isdecimal(), isnumeric()的区别和字符串的常用方法
# 全部小写 string.lower() # 全部大写 string.upper() # 是否全部小写 string.islower() # 是否全部大写 string.isupper() # 首字 ...
- elementUI实现前端分页
按照他的文档来写分页,最主要的是el-table里面展示的数据怎么处理 <el-table :data="AllCommodityList.slice((currentPage-1)* ...
- netsh常用命令
netsh常用命令 0.netsh介绍 netsh(Network Shell)是一个windows系统本身提供的功能强大的网络配置命令行工具 1.修改IP地址addr和子网掩码mask netsh ...
- Hyperledger Fabric CA User’s Guide——CA用户指南(一)
Fabric CA用户指南 Hyperledger Fabric CA是一种用于Hyperledger Fabric的认证机构(CA). 它提供了如下特性: 登记身份(注册ID),或者连接到作为用户注 ...
- VMware vSphere 6.0 安装及管理手册
目录 1. VMWARE_VSPHERE安装 1.1. 底层ESXI 安装步骤 1.2. VCENTER安装步骤 1) 准备vCenter安装环境 2) vCenter安装步骤 2. VMWARE_V ...
- [奇葩问题] Error Domain=NSURLErrorDomain Code=-1003
问题描述: 新上线的产品,ios同事拿着一串报错来找我,日志如下:err =Error Domain=NSURLErrorDomain Code=-1003 "未能找到使用指定主机名的服务器 ...
- 如何在静态方法或非Spring Bean中注入Spring Bean
在项目中有时需要根据需要在自己new一个对象,或者在某些util方法或属性中获取Spring Bean对象,从而完成某些工作,但是由于自己new的对象和util方法并不是受Spring所 ...
- Scrum Meeting 13 -2014.11.19
最近数据库和编译的实验课也开始了,大家晚上的时间直接被砍掉了大部分. 希望大家能顺利完成项目吧.剩下时间也不多了,如果程序还存在一些特别的问题和需要优化修改的地方也应该考虑留到下阶段进行了. Memb ...
- 【每日scrum】第一次冲刺day1
冲刺第一天,明确了自己的任务,数据分析与数据字典.