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 ...
随机推荐
- 微信小程序组件action-sheet
操作反馈action-sheet:官方文档 Demo Code: Page({ data: { actionSheetHidden: true, actionSheetItems: ['item1', ...
- ruby 修改Setting config yaml内容
参考:http://ruby-doc.org/stdlib-1.9.3/libdoc/yaml/rdoc/YAML/Store.html 本事例只为说明如何修改yml文件内容. 一.需求是怎么样修改y ...
- nginx日志输出,https,ssl
日志输出(浏览器直接访问)缺省安装下,浏览器是无法访问日志的,需要在编译的时候附带参数安装这些模块 ./configure --prefix=/usr/local/nginx --with-http_ ...
- 谷歌技术"三宝"之BigTable(转)
原文地址: http://blog.csdn.net/opennaive/article/details/7532589 2006年的OSDI有两篇google的论文,分别是BigTable和Ch ...
- C++编程中设置文件长度的方法
转自:http://blog.csdn.net/rrrfff/article/details/6705685 bool SetFileLength(const char *FilePath, off_ ...
- 20135302魏静静Linux内核分析第二周学习总结
操作系统是如何工作的 1. 小结:计算机是怎样工作的 三个法宝 存储程序计算机.函数调用堆栈.中断机制 两把宝剑 中断上下文.进程上下文的切换 2. 堆栈 堆栈是C语言程序运行时必须的一个记录调用路径 ...
- T-shirt again
T-shirt again 标签(空格分隔): 软工实践 第一次获得小黄裳是在大一下的C++课上,见T-shirt 0.0... 这次在软工课上能再次获得小黄裳,是我没有想到的,个人觉得里面有蛮多的运 ...
- 重新想,重新看——CSS3变形,过渡与动画②
本篇文章主要用来归纳总结CSS3变形属性. CSS3变形属性大致可以分为以下三个部分: 变形控制属性 2D变形函数 3D变形函数 下面将对其一一进行分析: 1.变形控制属性 所谓的变形控制属性主要指“ ...
- 【前端】javaScript 常用技巧总结
javaScript 常用技巧总结 1. 彻底屏蔽鼠标右键 oncontextmenu="window.event.returnValue=false" <table b ...
- Mac Book Air 上用 Vmware Fusion 8 pro 安装 CentOS7
一. 准备工作: 1. 安装Vmware Fusion (略) 2. 下载CentOS-7-x86_64-Minimal http://isoredirect.centos.org/centos/7/ ...