#254 Find the Longest Word in a String
找出最长单词
在句子中找出最长的单词,并返回它的长度。
函数的返回值应该是一个数字。
当你完成不了挑战的时候,记得开大招'Read-Search-Ask'。
这是一些对你有帮助的资源:
代码
function findLongestWord(str) {
// 按照空格分割字符串,生成数组
var strArr = str.split(" ");
// 初始化 length 为 0
var length = 0;
for (var i = 0; i < strArr.length; i++) {
// 遍历过程中,若当前字符串长度比 length 大,就更新 length
if (strArr[i].length > length) {
length = strArr[i].length;
}
// 不需要 else,因为如果比 length 小,继续执行遍历就可以了
}
// 循环结束,返回 length 作为结果
return length;
}
解释
- 要解释的不多,都在注释里。理解这个思路就好,在 for 循环外设置一个变量,用于追踪最大值。找到更大的就更新,最后返回这个变量就行
- 至于为什么不在 for 循环里设置变量,因为如果这样,执行循环的每一步都会重新初始化这个变量,我们就不能用这个变量来追踪循环过程了
优化
思路提示
- 可以优化的方案是,采用数组内置方法 reduce 来实现
参考链接
代码
function findLongestWord(str) {
var stringArr = str.split(" ");
return stringArr.reduce(function (prev, next) {
// 返回值为参数与当前字符串中较大的数
// 返回值会作为下次计算的 prev 传入
return Math.max(prev, next.length);
}, 0)
}
解释
- 如果不熟悉 reduce 语法和参数,请先去上面的链接看一下
- reduce 的第一个参数为回调,第二个参数为初始值。第二个参数可以为空
- 举个例子,如果我们传入的字符串是 "a bb ccc d" 执行过程如下:

- 因此,最终结果为 3
- 关于 reduce,一句话概括,就是:遍历数组,把上一次计算的结果用于下次计算
#254 Find the Longest Word in a String的更多相关文章
- freeCodeCamp:Find the Longest Word in a String
找到提供的句子中最长的单词,并计算它的长度. 函数的返回值应该是一个数字. /* 先把字符串 str 转为数组 myarr 将数组myarr中的每个元素长度转换成一个新的数组newarr 将这个数组按 ...
- Find the Longest Word in a String
找到提供的句子中最长的单词,并计算它的长度. 函数的返回值应该是一个数字. 这是一些对你有帮助的资源: String.split() String.length 第一种想法就是,先定一个小变量,来他一 ...
- 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 ...
随机推荐
- Linux Apache虚拟主机配置方法
apache 虚拟主机配置 注意: 虚拟主机可以开很多个 虚拟主机配置之后,原来的默认/etc/httpd/httpd.conf中的默认网站就不会生效了 练习: 主机server0 ip:172.25 ...
- 【转】Cisco交换机策略路由
[转自]https://blog.csdn.net/kkfloat/article/details/39940623 1.概念 1)策略路由(PBR)是一种比基于目标网络进行路由更加灵活的数据包路由转 ...
- python note 13 内置函数
1. lst = ["白蛇传","骷髅叹","庄周闲游"] it = lst.__iter__() print(it.__next__()) ...
- Vue源码学习(二)$mount() 后的做的事(1)
Vue实例初始化完成后,启动加载($mount)模块数据. (一)Vue$3.protype.$mount 标红的函数 compileToFunctions 过于复杂,主要是生 ...
- scrapy Mongodb 储存
pipelines.py # -*- coding: utf-8 -*- # Define your item pipelines here # # Don't forget to add your ...
- vim 中文乱码怎么解决
一般来说只需要正确设置vim的编码识别序列就很少会遇到乱码问题: set fileencodings=ucs-bom,utf-8,utf-16,gbk,big5,gb18030,latin1 这个设置 ...
- linux下编译protobuf(可以编译成pb.go)
编译前需要安装gtest $ cd googletest $ cmake -DBUILD_SHARED_LIBS=ON . $ make $ sudo cp -a include/gtest /hom ...
- 面试简单整理之web
63.servlet是什么?运行过程? Servlet是一门用于开发动态web资源的技术. 运行过程: Servlet程序是由WEB服务器调用,web服务器收到客户端的Servlet访问请求后: ①W ...
- grunt压缩js代码
安装node.js的环境和grunt插件在上一篇已经将过,点击这里跳到上一篇 所以我们直接从压缩插件的安装开始讲 起 1.安装uglify插件 目录结构如下: 命令行:npm install grun ...
- Eclipse 中 Maven 项目默认JDK版本为1.5 的解决方法
在 Eclipse 中 Maven project 的默认 JDK 版本是 1.5, 如果不在 settings.xml 或者 pom.xml 中显示的指出 JDK 版本,每次 右键项目--> ...