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 ...
随机推荐
- Linux下mysql的安装和使用(C语言)
1 mysql的安装 我使用的ubuntu在线安装,非常简单,命令为: sudo apt-get install mysql-client mysql-server 2 mysql命令集合 网络太多了 ...
- js实现checkbox的全选/取消
所有的操作都将使用jquery进行. 主要是为了实现指定内容的批量/单独删除操作. 先看一下页面的设计. 实现操作的主要地方是: 首先实现单击“标题”旁的checkbox实现所有条目的选择. 要点:j ...
- Android:什么是Holo?【Translated By KillerLegend】
Android:什么是Holo? Martin Brinkmann on May 6, 2013 in Google Android 3 [Translated By KillerLegend] 当你 ...
- GoldenGate 基础架构
一.Goldengate 产品家庭 Goldengate:核心产品 Goldengate Director :现已更名为Goldengate Management Pack,为Goldengate提供 ...
- Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_100_CI_AS" in the equal to operation.
ErrorMessage Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" ...
- Visual Studio
1.必备神器http://www.cnblogs.com/stoneniqiu/p/3488546.html 2.常用快捷键http://www.cnblogs.com/TankXiao/p/3468 ...
- Go在linux下的安装
在Ubuntu.Debian 或者 Linux Mint上安装Go语言 下面是在基于Debian的发行版上使用apt-get来安装Go语言和它的开发工具. $ sudo apt-get install ...
- 从零开始学ios开发(九):Swapping Views
这篇的内容是切换Views,也是上一篇中提到的第三种当iphone发生旋转后改变布局的方式,先回顾一下上一篇中提到的三种方式 1.使用Autosizing 2.写code 3.重新弄个View,替换原 ...
- PHP正则表达式的逆向引用与子模式 php preg_replace应用
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit]) 功能 在 subject 中搜索 ...
- mysql卸载
先执行mysql安装程序,点击移除,然后再删除对应的安装路径,必要的时候还要删除注册表信息.