Find the Longest Word in a String
找到提供的句子中最长的单词,并计算它的长度。
函数的返回值应该是一个数字。
这是一些对你有帮助的资源:
第一种想法就是,先定一个小变量,来他一个0;然后把每个单词的长度与它比较,把长度大的赋值给该变量,最后返回该变量;
function findLongestWord(str) {
var array=str.split(' '); //分割句子为单词,保存在数组array里
var result=0; //先定一个小目标
for(var i=1;i<array.length;i++){ //遍历单词
if (result<=array[i].length) { // 单词长度与result比较
result=array[i].length; //长单词长度赋值给result并进行下一次比较
}
}
return result; // 返回结果
}
第二种方法就是利用数组sort()方法,sort()方法需要接受一个比较函数,该函数要比较两个值,然后返回一个用于说明这两个值的相对顺序的数字。比较函数应该具有两个参数 a 和 b,其返回值如下:
- 若 a 小于 b,在排序后的数组中 a 应该出现在 b 之前,则返回一个小于 0 的值。
- 若 a 等于 b,则返回 0。
- 若 a 大于 b,则返回一个大于 0 的值。
然后我们就能实现下面的 解法
function findLongestWord(str) {
var arry=str.split(' ');
arry.sort(function(f,n){
return n.length-f.length;
});
return arry[0].length;
}
第三种方法,直接O(n)时间复杂度实现:
function findLongestWord(str) {
var max = 0;
var temp = 0;
for(var i=0;i<str.length;i++){
temp++;
if(str[i]==' '){
if(temp>max){
max = temp-1;
}
temp = 0;
}
}
if(temp>max){
max = temp;
}
return max;
}
Find the Longest Word in a String的更多相关文章
- freeCodeCamp:Find the Longest Word in a String
找到提供的句子中最长的单词,并计算它的长度. 函数的返回值应该是一个数字. /* 先把字符串 str 转为数组 myarr 将数组myarr中的每个元素长度转换成一个新的数组newarr 将这个数组按 ...
- #254 Find the Longest Word in a String
找出最长单词 在句子中找出最长的单词,并返回它的长度. 函数的返回值应该是一个数字. 当你完成不了挑战的时候,记得开大招'Read-Search-Ask'. 这是一些对你有帮助的资源: String. ...
- FCC JS基础算法题(3):Find the Longest Word in a String (找出最长单词)
题目描述: 在句子中找出最长的单词,并返回它的长度.函数的返回值应该是一个数字. 基本思路,将字符串转换成数组,然后得出数组中单个元素的长度,对长度进行排序,返回最大的一个 代码: function ...
- Find the Longest Word in a String-freecodecamp算法题目
Find the Longest Word in a String(找出最长单词) 要求 在句子中找出最长的单词,并返回它的长度 函数的返回值应该是一个数字. 思路 用.split(' ')将句子分隔 ...
- [CareerCup] 18.7 Longest Word 最长的单词
5.7 Given a list of words, write a program to find the longest word made of other words in the list. ...
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- [LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词
Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...
- [Swift]LeetCode524. 通过删除字母匹配到字典里最长单词 | Longest Word in Dictionary through Deleting
Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...
- [Swift]LeetCode720. 词典中最长的单词 | Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
随机推荐
- Tomcat内部结构及请求原理(转)
Tomcat Tomcat是一个JSP/Servlet容器.其作为Servlet容器,有三种工作模式:独立的Servlet容器.进程内的Servlet容器和进程外的Servlet容器. Tomcat的 ...
- radio,checkbox,select,input text获取值,设置哪个默认选中
11 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title& ...
- 127. Word Ladder(单词变换 广度优先)
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- 如何通过包名打开手机里的APP
目前已知的打开APP的方式有两种, 一种是通过openUrl打开,这种有一个严重的问题,即必须添加白名单,白名单之外的APP即时安装了也无法打开. 另一种就是今天的重点,通过包名打开APP.先上核心代 ...
- 论文笔记:空间变换网络(Spatial Transformer Networks)
2015, NIPS Max Jaderberg, Karen Simonyan, Andrew Zisserman, Koray Kavukcuoglu Google DeepMind 为什么提出( ...
- 论文笔记:蒸馏网络(Distilling the Knowledge in Neural Network)
Distilling the Knowledge in Neural Network Geoffrey Hinton, Oriol Vinyals, Jeff Dean preprint arXiv: ...
- Spring AOP统一异常处理
1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- head中的title显示在body中
今天遇到一个问题,就是title中的内容会显示在body中 <head> <title>324234</title> </head> 网上搜了一下是说编 ...
- Oracle中用sql语句取随机数和整数
--- 应用round(5.678,3)保留小数 应用floor(5.678)保留整数 应用dbms_random.value(30,50)取得随机数 --- dbms_random包 获得随机小数S ...
- [pixhawk笔记]5-uORB消息传递
本文主要内容翻译自官方文档:https://dev.px4.io/en/middleware/uorb.html 在前一篇笔记中使用uORB完成消息传递,实现了一个简单示例程序,本文将对uORB进行系 ...