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 ...
随机推荐
- Javascript原型链
原型链的关系 在Javascript中,只要创建了一个新函数,就会为该函数创建prototype属性,指向函数的原型对象,Object.prototype是所有对象最顶层的原型.所有对象都继承由Obj ...
- mysql 让一个存储过程定时作业的代码
1.在mysql 中建立一个数据库 test1 语句:create database test1 2.创建表examinfo create table examinfo( id int auto_in ...
- LLVM language 参考手册(译)(2)
调用约定(Calling Conventions) LLVM functions, calls and invokes 可以带有一个可选的调用约定来指明调用方式.每一对 caller/callee(调 ...
- How to: Enable and Disable an Action Pane Button on a List Page [AX 2012]
Applies To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynami ...
- WPF 界面控件遍历和后台行为绑定写法
InvokeCommandAction ic = new InvokeCommandAction(); ic.Command = tree.SelectedItemCommand;//绑定的命令 ic ...
- ORA-14404
OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - ...
- ASP.NET操作WMI
WMI Functions from ASP.NET Introduction This article demonstrates how to use WMI in ASP.NET to cre ...
- 【转】- 使用T4模板批量生成代码
前言 之前在 “使用T4模板生成代码 - 初探” 文章简单的使用了T4模板的生成功能,但对于一个模板生成多个实例文件,如何实现这个方式呢?无意发现一个解决方案 “MultipleOutputHelpe ...
- When to use Class.isInstance() & when to use instanceof operator?
I think the official documentation gives you the answer to this one (albeit in a fairly nonspecific ...
- Python Socket File Transfer
I have a RPi which I intented to use it to crawl data. The development environment in RPi is very ba ...