leetcode720
public class Solution
{
public string LongestWord(string[] words)
{
var maxlist = new List<string>(); var dic = new Dictionary<string, string>();
Queue<string> Q = new Queue<string>();
var maxstr = ""; foreach (var word in words)
{
var temp = word;
if (!dic.ContainsKey(temp))
{
dic.Add(temp, temp.Substring(, temp.Length - ));
}
}
var list = dic.OrderByDescending(x => x.Key.Length).ToList(); foreach (var l in list)
{
var cur = l.Key;
if (cur.Length < maxstr.Length)
{
continue;
} Q.Enqueue(cur);
var len = cur.Length;
while (Q.Any())
{
len--;
var temp = Q.Dequeue();
var subtemp = temp.Substring(, temp.Length - );
if (dic.ContainsKey(subtemp))
{
Q.Enqueue(subtemp);
}
} if (len == )
{
maxlist.Add(cur);
maxstr = cur;
}
} var result = maxlist.OrderBy(x => x).ToList().FirstOrDefault();
return result;
}
}
leetcode720的更多相关文章
- [Swift]LeetCode720. 词典中最长的单词 | Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- Leetcode720.Longest Word in Dictionary词典中最长的单词
给出一个字符串数组words组成的一本英语词典.从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成.若其中有多个可行的答案,则返回答案中字典序最小的单词. 若无答案,则返回 ...
随机推荐
- CFE Bootloader详解 — 引导过程
CFE命令 CFE引导过程 系统加电后,CFE从boot.S (src/shared/boot.S)开始执行,完成判断芯片类型.设置时钟.初始化缓存.把自身加载进RAM等任务后,跳转到c_main() ...
- mongo 指令
简单查询1: db.find( {}, {} ) 第一个{} 是条件,第二个{}是需要那些属性, db.find( {} ) 第二个{}没有,代表返回所有属性 db.find( {age: ...
- Codeforces 1012C Hills【DP】*
Codeforces 1012C Hills Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suff ...
- 《selenium2 python 自动化测试实战》(5)——键盘事件
键盘事件,就是键盘上的一些操作,比如Ctrl +C,Ctrl+V,Ctrl+X等. 对键盘的操作需要导入另一个键盘的库: from selenium.webdriver.common.keys imp ...
- vue的路由初识01
今天就做了一个vue-router的实例,(路由跳转,参数的传递[一个参数,多个参数])<!DOCTYPE html> <html> <head> <meta ...
- jquery 操作单选按钮
<input type="radio" name="sex" value="男" />男 <input type=&quo ...
- 启动Oracle数据库时报错ORA-00119 & ORA-00132
今天启动Oracle数据库时报错ORA-00119 & ORA-00132,找到解决方法做个记录,方便日后查看. 若是ORACLE不提示错误的话,可以自己查看ORACLE的日志文件. Orac ...
- Unit01: JDBC原理 、 JDBC基础编程
Unit01: JDBC原理 . JDBC基础编程 这个文件里面有两块内容: 1.用比较麻烦的方式连接数据库,test1(),test4() 2.创建DBTool,测试DBTool连接数据库 ,tes ...
- native方法
看到虚拟机栈和本地方法栈的区别的时候有点疑惑,因为本地方法栈为虚拟机的Native方法服务.以下转载一篇关于native方法的介绍: http://blog.csdn.net/wike163/arti ...
- 简单的SOCKET例子
定义实例socket.socket(),如果括号里不写参数,默认为IPV4+TCP 我们猜测客户端的完整代码如下: 同样我们猜测服务端的代码如下: 实际上运行客户端代码: 说明在py3里,网络编程发送 ...