problem

720. Longest Word in Dictionary

题意:

solution1: BFS;

class Solution {
public:
string longestWord(vector<string>& words) {
string res = "";
unordered_set<string> s(words.begin(), words.end());
queue<string> q;
for(auto word:words)
{
if(word.size()==) q.push(word);
}
int maxLen = ;
while(!q.empty())
{
string t = q.front();
q.pop();
if(t.size()>maxLen)
{
maxLen = t.size();
res = t;//err....
}
else if(t.size()==maxLen) res = min(res, t);
for(char ch='a'; ch<='z'; ++ch)//err...
{
t.push_back(ch);
if(s.count(t)) q.push(t);
t.pop_back();
}
}
return res;
}
};

solution2:

class Solution {
public:
string longestWord(vector<string>& words) {
string res = "";
unordered_set<string> s(words.begin(), words.end());
int maxLen = ;
for (auto word:words)
{
if(word.size()==) helper(s, word, maxLen, res);
}
return res;
}
void helper(unordered_set<string>& s, string word, int& maxLen, string& res) {
if(word.size()>maxLen)
{
maxLen = word.size();
res = word;
}
else if(word.size()==maxLen) res = min(res, word);
for(char ch = 'a'; ch<='z'; ++ch)
{
word.push_back(ch);
if(s.count(word)) helper(s, word, maxLen, res);
word.pop_back();
}
}
};

参考

1. Leetcode_easy_720. Longest Word in Dictionary;

2. Grandyang;

【Leetcode_easy】720. Longest Word in Dictionary的更多相关文章

  1. 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...

  2. [LeetCode&Python] Problem 720. Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  3. leetcode 720. Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  4. 720. Longest Word in Dictionary 能连续拼接出来的最长单词

    [抄题]: Given a list of strings words representing an English Dictionary, find the longest word in wor ...

  5. LeetCode 720. Longest Word in Dictionary (字典里最长的单词)

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  6. 【Leetcode_easy】953. Verifying an Alien Dictionary

    problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...

  7. 【Leetcode_easy】687. Longest Univalue Path

    problem 687. Longest Univalue Path 参考 1. Leetcode_easy_687. Longest Univalue Path; 2. Grandyang; 完

  8. 【Leetcode_easy】674. Longest Continuous Increasing Subsequence

    problem 674. Longest Continuous Increasing Subsequence solution class Solution { public: int findLen ...

  9. 【Leetcode_easy】594. Longest Harmonious Subsequence

    problem 594. Longest Harmonious Subsequence 最长和谐子序列 题意: 可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列 ...

随机推荐

  1. BZOJ3678 wangxz与OJ (平衡树 无旋treap)

    题面 维护一个序列,支持以下操作: 1.在某个位置插入一段值连续的数. 2.删除在当前序列位置连续的一段数. 3.查询某个位置的数是多少. 题解 显然平衡树,一个点维护一段值连续的数,如果插入或者删除 ...

  2. LeetCode 339. Nested List Weight Sum

    原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...

  3. 自定义简单算法MVC框架

    什么是MVC框架 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,   它是一种软件设计典范,用一种业务逻辑.数据 ...

  4. react图片预览插件尝试

    npm install react-zmage -S https://blog.csdn.net/Wcharles666/article/details/90262525 启动报错 直接执行  npm ...

  5. CF359D Pair of Numbers gcd+暴力

    利用区间 gcd 个数不超过 log 种来做就可以了~ code: #include <bits/stdc++.h> #define N 300005 #define setIO(s) f ...

  6. Chinese Mahjong

    OJ题号:UVa11210 思路: 首先字符串处理读入手牌,str数组将手牌和数字对应,接下来搜索,先搜对子,如果搜过对子就不搜了.由于对子有且只有一个,可以在搜到以后直接跳出.同时注意一副麻将中每种 ...

  7. 题解 CF1063B 【Labyrinth】

    题解 CF1063B [Labyrinth] 完了我发现我做CF的题大部分思路都和别人不一样qwq 这道题其实很水,不至于到紫题 我们只要bfs一下,向四个方向剪下枝,就A了(好像还跑的蛮快?) 是一 ...

  8. Java 8的Time包常用API

    Date.Canlender.SimpleDateFormat类在新的Time包面前几乎没有优势 日期LocalDate,时间LocalTime,日期时间LocalDateTime. 时区ZoneId ...

  9. C++中的平方、开方、绝对值怎么计算

    #include <math.h> //平方 pow() ,);// 4的平方=16 //开方 ,0.5);// 4的平方根=2 );// 4的平方根=2 //整数绝对值 int c = ...

  10. meshing-simple_block

    原视频下载地址:https://yunpan.cn/cqjeSzP7s93Pc  访问密码 aaff