找到提供的句子中最长的单词,并计算它的长度。

函数的返回值应该是一个数字。

这是一些对你有帮助的资源:

第一种想法就是,先定一个小变量,来他一个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的更多相关文章

  1. freeCodeCamp:Find the Longest Word in a String

    找到提供的句子中最长的单词,并计算它的长度. 函数的返回值应该是一个数字. /* 先把字符串 str 转为数组 myarr 将数组myarr中的每个元素长度转换成一个新的数组newarr 将这个数组按 ...

  2. #254 Find the Longest Word in a String

    找出最长单词 在句子中找出最长的单词,并返回它的长度. 函数的返回值应该是一个数字. 当你完成不了挑战的时候,记得开大招'Read-Search-Ask'. 这是一些对你有帮助的资源: String. ...

  3. FCC JS基础算法题(3):Find the Longest Word in a String (找出最长单词)

    题目描述: 在句子中找出最长的单词,并返回它的长度.函数的返回值应该是一个数字. 基本思路,将字符串转换成数组,然后得出数组中单个元素的长度,对长度进行排序,返回最大的一个 代码: function ...

  4. Find the Longest Word in a String-freecodecamp算法题目

    Find the Longest Word in a String(找出最长单词) 要求 在句子中找出最长的单词,并返回它的长度 函数的返回值应该是一个数字. 思路 用.split(' ')将句子分隔 ...

  5. [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. ...

  6. [LeetCode] Longest Word in Dictionary 字典中的最长单词

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  7. [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 ...

  8. [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 ...

  9. [Swift]LeetCode720. 词典中最长的单词 | Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

随机推荐

  1. 3-CMD

    诞生背景AMD其实已经算是解决了前端模块化开发的问题了,但是!!!但是,因为AMD管理依赖的时候,会把所有的依赖下载到本地,并且立即执行一次.依赖少了还好,多的话,那么多js均要被执行一次,这个内存消 ...

  2. CodeForces - 528D Fuzzy Search (FFT求子串匹配)

    题意:求母串中可以匹配模式串的子串的个数,但是每一位i的字符可以左右偏移k个位置. 分析:类似于 UVALive -4671. 用FFT求出每个字符成功匹配的个数.因为字符可以偏移k个单位,先用尺取法 ...

  3. [Face++]Face初探——人脸检测

    经过了强烈的思想斗争才把自己拖到图书馆做毕设T^T anyway, 因为毕设里面有人脸识别的部分,所以就想找个现成的api先玩玩,于是就找到最近很火的face++:http://www.faceplu ...

  4. LINUX SHELL 笔记 01: 脚本

    root@iZwz:~/labs# vim myfirst root@iZwz:~/labs# cat myfirst #!/bin/bash clear echo "this is my ...

  5. git命令学习汇总

    GIT 版本控制常用命令汇总 git version 查看当前git版本信息 git help 获取全部命令帮助信息 git help <command> 获取指定命令帮助信息 git c ...

  6. hive + hadoop 环境搭建

    机器规划: 主机 ip 进程 hadoop1 10.183.225.158 hive server hadoop2 10.183.225.166 hive client 前置条建: kerberos部 ...

  7. Vue数据绑定失效

    首先,我们得明白Vue数据响应的原理: 以对象为例:当把一个JavaScript对象传给Vue实例的data选项时,Vue将遍历此对象所有的属性,并使用Object.defineProperty把这些 ...

  8. 20145211《网络渗透》MS12-004漏洞渗透

    20145211<网络渗透>MS12-004漏洞渗透 一 实验原理 初步掌握平台matesploit的使用 有了初步完成渗透操作的思路 在这里我选择对的不是老师推荐的MS11_050,而是 ...

  9. [转]C语言 gets()和scanf()函数的区别

    scanf( )函数和gets( )函数都可用于输入字符串,但在功能上有区别.若想从键盘上输入字符串"hi hello",则应该使用__gets__函数. gets可以接收空格:而 ...

  10. mysql增加远程访问

    grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;