lintcode :最长单词
题目:
给一个词典,找出其中所有最长的单词。
在词典
{
"dog",
"google",
"facebook",
"internationalization",
"blabla"
}
中, 最长的单词集合为 ["internationalization"]
在词典
{
"like",
"love",
"hate",
"yes"
}
中,最长的单词集合为 ["like", "love", "hate"]
遍历两次的办法很容易想到,如果只遍历一次你有没有什么好办法?
解题:
其实这个题目如果不是在这里做,我只能会暴力了,但是lintcode给你了需要返回的值,这个是很大的提示,这个题返回类型是ArrayList<String> ,然后就有思路了。值选取比较长的字符,加入list中,当碰到更长的时候可以清空list。
Java程序:
class Solution {
/**
* @param dictionary: an array of strings
* @return: an arraylist of strings
*/
ArrayList<String> longestWords(String[] dictionary) {
// write your code here
ArrayList<String> result = new ArrayList<String>();
if( dictionary.length==0)
return result;
int dictlen = dictionary[0].length();
result.add(dictionary[0]);
for( int i = 1;i<dictionary.length;i++){
String tmp = dictionary[i];
if( tmp.length()==dictlen){
result.add( tmp );
}else if(tmp.length()> dictlen){
result.clear();
result.add(tmp);
dictlen = tmp.length();
}
}
return result;
}
};
总耗时: 1798 ms
Python程序:
class Solution:
# @param dictionary: a list of strings
# @return: a list of strings
def longestWords(self, dictionary):
# write your code here
m = len( dictionary )
result = []
if m ==0:
return result
dictlen = len(dictionary[0])
result.append(dictionary[0])
for d in range(1,m):
tmp = dictionary[d]
if len(tmp) == dictlen:
result.append(tmp)
elif len(tmp)> dictlen:
result = []
dictlen = len(tmp)
result.append(tmp)
return result
总耗时: 405 ms
lintcode :最长单词的更多相关文章
- LintCode之最长单词
题目描述: 分析:先建一个数组s用来存储每个字符串的长度,然后遍历数组s得到最大的数max,这个数就是词典中的最长单词的长度,由于可能有多个长度相等的单词,所以要循环整个词典,当一个单词的长度等于ma ...
- lintcode-133-最长单词
133-最长单词 给一个词典,找出其中所有最长的单词. 样例 在词典 { "dog", "google", "facebook", &quo ...
- OpenJudge就算概论-最长单词2【寻找句子内部最长的单词】
/*===================================== 最长单词2 总时间限制: 1000ms 内存限制: 65536kB 描述 一个以'.'结尾的简单英文句子,单词之间用空格 ...
- 英文长单词断行 word-break VS word-wrap
你真的了解word-wrap和word-break的区别吗? 这两个东西是什么,我相信至今还有很多人搞不清,只会死记硬背的写一个word-wrap:break-word;word-break:brea ...
- CSS3让长单词与URL地址自动换行——word-wrap属性
div{ word-wrap:break-word; } word-wrap属性可以使用的属性值为normal与break-word两个.使用normal属性值时浏览器默认处理,只在半角空格或者连字符 ...
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- 允许长单词、数字、URL换行到下一行
CSS3 word-wrap 属性 normal 只在允许的断字点换行(浏览器保持默认处理) break-word 在长单词.数字.URL地址内部进行换行 页面效果图: 源码:
- C语言 · 最长单词
算法提高 最长单词 时间限制:1.0s 内存限制:512.0MB 编写一个函数,输入一行字符,将此字符串中最长的单词输出. 输入仅一行,多个单词,每个单词间用一个空格隔开.单词仅由小 ...
- word-wrap与word-break为长单词换行
如果你遇到长串英文单词或者url换行的问题,这时候就需要用到word-wrap与word-break这2个css属性啦. word-wrap:break-word;长单词与url地址自动换行. wor ...
随机推荐
- file与 byte[] 互转
byte 转file String filepath="D:\\"+getName(); File file=new File(filepath); ...
- elr_memory_pool详解
Preface Usually, memory allocation of OS is fast, especially the computer has just started. But over ...
- keditor_php图片上传
<script type="text/javascript" src="/statics/js/kindeditor/kindeditor-min.js" ...
- CSS3 animation小动画
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 在HTML中通过jQuery设置列表项符号
在创建列表的时候,可以通过指定type来设置列表项的符号,如下所示: <body> <form id="form1" runat="server&quo ...
- 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表中的标识列指定显式值
今天在处理数据时遇到这样一个错误 消息 8101,级别 16,状态 1,第 1 行 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'dbo.StockDetailValu ...
- [SQL_Server_Question]Msg 1105无法为数据库 'tempdb' 中的对象分配空间,因为 'PRIMARY' 文件组已满
错误消息: Msg 1105, Level 17, State 2, Line 266Could not allocate space for object 'dbo.Large Object Sto ...
- Underscore 源码
Underscore 源码 作者:韩子迟 What? 不知不觉间,「Underscore 源码解读系列」进入了真正的尾声,也请允许我最后一次 po 下项目的原始地址 https://github.co ...
- javascript之流程控制 和函数的容易忽略点
1.流程控制 1> for in 仅用于 对象的遍历: var box={ "name":'小红', 'age':18, 'height':165 }; for(var b ...
- linux第四周作业
一.用户态内核态与中断 1.库函数把内核调用封装起来. 2.区分内核态和用户态是为了让系统更稳定.Linux里吧用户态定位3级,把内核态定位0级. 3.中断处理就是从用户态进入内核态的主要方法,系统调 ...