题目内容:Write a function to find the longest common prefix string amongst an array of strings

题目分析:本题目利用字符串数组中的一个字符串作为参考,数组中的所有字符串都和该字符串进行对比,找出所有字符串开头出现字符串的交集。其中用到的是字符串的indexOf函数。

定义和用法

indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。

语法

stringObject.indexOf(searchvalue,fromindex)
参数 描述
searchvalue 必需。规定需检索的字符串值。
fromindex 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的首字符开始检索。

说明

该方法将从头到尾地检索字符串 stringObject,看它是否含有子串 searchvalue。开始检索的位置在字符串的 fromindex 处或字符串的开头(没有指定 fromindex 时)。如果找到一个 searchvalue,则返回 searchvalue 的第一次出现的位置。stringObject 中的字符位置是从 0 开始的。

提示和注释

注释:indexOf() 方法对大小写敏感!

注释:如果要检索的字符串值没有出现,则该方法返回 -1。

实例

在本例中,我们将在 "Hello world!" 字符串内进行不同的检索:

<script type="text/javascript">

var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")
document.write(str.indexOf("World") + "<br />")
document.write(str.indexOf("world")) </script>

以上代码的输出:0 -1 6

题目代码:

14. Longest Common Prefix ★的更多相关文章

  1. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  2. 14. Longest Common Prefix【leetcode】

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  3. Leetcode 14. Longest Common Prefix(水)

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  4. leetCode练题——14. Longest Common Prefix

    1.题目 14. Longest Common Prefix   Write a function to find the longest common prefix string amongst a ...

  5. C# 写 LeetCode easy #14 Longest Common Prefix

    14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  6. [LeetCode] 14. Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  7. 14. Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...

  8. [LeetCode] 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. public class ...

  9. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  10. 【LeetCode】14 - Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...

随机推荐

  1. Oracle使用——oracle11g安装——Oracle要求的结果: 5.0,5.1,5.2,6.0 6.1 之一 实际结果: 6.2

    问题 正在检查操作系统要求...        要求的结果: 5.0,5.1,5.2,6.0 之一        实际结果: 6.1        检查完成.此次检查的总体结果为: 失败 <&l ...

  2. tomcat 监控脚本

    ps -ef |grep tomcat |grep -w 'atlassian'|grep -v 'grep'|awk '{print $2}' curl -s -o /dev/null -m 10 ...

  3. 关于PHP5.6连接SqlServer

    在做一个PHP报名系统的时候需要使用mssql来和winform结合起来使用, 但是发现我的php环境没有sqlsrv模块,于是乎,我就开始百度了 找到了微软官方下载地址,对照php版本,下载对应的模 ...

  4. C# 数组与集合的区别

    数组:声明数组的时候必须指定数组的长度.数组在内存中是连续的,索引速度很快,赋值和修改也很简单. 数组的容量是固定的,您只能一次获取或设置一个元素的值,而ArrayList或List的容量可根据需要自 ...

  5. shell编程(五)之函数

    function:函数 函数只有被调用才会执行如何调用:给定函数名 函数名出现的地方,会被自动替换为函数代码 函数的生命周期:被调用时创建,返回时终止return命令返回自定义状态结果 0:成功 1- ...

  6. 加sogou 和wps

    刚装完系统,装常用工具: sogoupinyin / wps linux ==========sogoupinyin=========================1.sogou linux 下载d ...

  7. 2019 年 SpreadJS产品路线图(Roadmap)

    前言 | 问题背景 SpreadJS已经发展成为市场上最好的纯前端JavaScript电子表格之一,但对于用户的需要和产品功能的改善,我们才刚刚开始! 在过去的一年中,我们发布了两个Service P ...

  8. 【BUAA-OO】第二单元作业总结

    第二单元作业总结 ——电梯恐惧症患者的极限自救 一.   第一次作业程序分析 1.     设计策略简略分析 线程:主线程.输入线程和电梯线程,另有一个持有请求队列的调度器,一个对输入进行处理的Req ...

  9. hp quicktestprofession ver-10.0(QTP)的入门使用指南

    ---恢复内容开始--- SQA(software quality assurance) tool hp quicktestprofession ver-10.0(QTP) environment w ...

  10. openstack-ntp时间同步服务

    ntp时钟同步服务 NTP 工作请求 (1) 客户端将采用随机端口向 NTP 服务器 (UDP:123) 发出时间同步请求 (2)NTP 服务器收到请求后会将发出调校时间 (3)NTP 客户端接收到 ...