LintCode之最长单词
题目描述:


分析:先建一个数组s用来存储每个字符串的长度,然后遍历数组s得到最大的数max,这个数就是词典中的最长单词的长度,由于可能有多个长度相等的单词,所以要循环整个词典,当一个单词的长度等于max时,就将它存到要返回的ArrayList中。
我的代码:
public class Solution {
/*
* @param dictionary: an array of strings
* @return: an arraylist of strings
*/
public ArrayList<String> longestWords(String[] dictionary) {
// write your code here
ArrayList<String> l = new ArrayList<String>();
int max=0;
int[] s = new int[dictionary.length];
for(int i=0; i<dictionary.length; i++) {
s[i] = dictionary[i].length();
}
for(int i=0; i<s.length; i++) {
if(max < s[i]) {
max = s[i];
}
}
for(int i=0; i<s.length; i++) {
if(max == s[i]) {
l.add(dictionary[i]);
}
}
return l;
}
}
LintCode之最长单词的更多相关文章
- lintcode :最长单词
题目: 最长单词 给一个词典,找出其中所有最长的单词. 样例 在词典 { "dog", "google", "facebook", &quo ...
- 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 ...
随机推荐
- web 前端1 html5基础
HTML web sockent 实例 import socket def handle_request(client): buf = client.recv(1024) client.sendall ...
- 版本控制工具 GIT 简要教程
一,Git 简介 其实这个就不用说了 但是国际惯例还是介绍一下吧; Git 是一个开源的分布式版本控制系统,用于敏捷 高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助 ...
- SQL 中的正则函数
ORACLE中支持正则表达式的函数主要有下面四个: 1,REGEXP_LIKE :与LIKE的功能相似,比LIKE强大得多. 2,REGEXP_INSTR :与INSTR的功能相似. 3,REGEXP ...
- Java OO知识总结
接着上一集 https://www.cnblogs.com/pdev/p/11288014.html 5. OO中的一些基本概念 继承 父类的非private方法自动被子类继承 cl ...
- .net easyui Tree树
原文:https://www.cnblogs.com/hantianwei/archive/2012/03/19/2407118.html Tree 树 用 $.fn.tree.defaults ...
- 20191115PHP cookie登入实例
首先是登入页面 <form action="" method="post"> <input type="text" nam ...
- 奇葩问题:Invalid bound statement (not found): cn.zss.zsdemo.mapper.RoleMapper.selectByPrimaryKey
使用mybatis,遇到Invalid bound statement (not found): cn.zss.zsdemo.mapper.RoleMapper.selectByPrimaryKey ...
- 定制团队自己的 Vue template
一,我们使用vue-cli 可以快速初始化vue.js的项目,官方提供了webpack,pwa,browserify-sipmple,等常用template 二.置知识1,模板结构template:该 ...
- webpack webpack.config.js配置
安装指定版本的webpack npm install webpack@3.6 -g 安装live-server 运行项目插件 输入live-server 运行后自动打开网页 npm ins ...
- python基础--内置函数map
num_1=[1,2,10,5,3,7] # num_2=[] # for i in num_1: # num_2.append(i**2) # print(num_2) # def map_test ...