博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Leetcode_easy】720. Longest Word in Dictionary
阅读量:4330 次
发布时间:2019-06-06

本文共 1693 字,大约阅读时间需要 5 分钟。

problem

题意:

solution1: BFS;

class Solution {public:    string longestWord(vector
& words) { string res = ""; unordered_set
s(words.begin(), words.end()); queue
q; for(auto word:words) { if(word.size()==1) q.push(word); } int maxLen = 0; 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
& words) { string res = ""; unordered_set
s(words.begin(), words.end()); int maxLen = 0; for (auto word:words) { if(word.size()==1) helper(s, word, maxLen, res); } return res; } void helper(unordered_set
& 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. ;

2. ;

转载于:https://www.cnblogs.com/happyamyhope/p/11114849.html

你可能感兴趣的文章
spring第二冲刺阶段第七天
查看>>
搜索框键盘抬起事件2
查看>>
阿里百川SDK初始化失败 错误码是203
查看>>
透析Java本质-谁创建了对象,this是什么
查看>>
BFS和DFS的java实现
查看>>
关于jquery中prev()和next()的用法
查看>>
一、 kettle开发、上线常见问题以及防错规范步骤
查看>>
eclipse没有server选项
查看>>
CRC码计算及校验原理的最通俗诠释
查看>>
QTcpSocket的连续发送数据和连续接收数据
查看>>
使用Gitbook来编写你的Api文档
查看>>
jquery扩展 $.fn
查看>>
Markdown指南
查看>>
influxDB的安装和简单使用
查看>>
JPA框架学习
查看>>
JPA、JTA、XA相关索引
查看>>
机器分配
查看>>
php opcode缓存
查看>>
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
Go 结构体
查看>>