题目内容: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. JSESSIONID的简单说明

    原文地址:http://blog.csdn.net/chunqiuwei/article/details/23461995 1)第一次访问服务器的时候,会在响应头里面看到Set-Cookie信息(只有 ...

  2. SKCTF Writeup

    签到题 请打开微信关注,发送give me flag,即可获得. Encode 1.ACSCLL 首先看到这类题,我们肯定是要使用ASCLL的(这么明显的提示大家肯定一眼就能看出来),我们可以对照As ...

  3. 使用代理实现对C# list distinct操作

    范型在c#编程中经常使用,而经常用list 去存放实体集,因此会设计到对list的各种操作,比较常见的有对list进行排序,查找,比较,去重复.而一般的如果要对list去重复如果使用linq dist ...

  4. pyqt5安装与pycharm配置

    最近几天新入坑了python的GUI设计,回想一下我为什么会入门这个???好像是在知乎上看到你都用 Python 来做什么? 这篇文章,看到有人回答说将python打包成exe文件,然后就想把之前弄得 ...

  5. Java面试题之Java基础

    1. JDK 和 JRE 有什么区别? JRE(JavaRuntimeEnvironment,Java运行环境),也就是Java平台.所有的Java 程序都要在JRE下才能运行.普通用户只需要运行已开 ...

  6. Go语言学习之15 商品秒杀开发与接入层实现

    outline 1. 秒杀抢购接入层实现2. 秒杀逻辑层实现 秒杀接入层核心功能 秒杀逻辑层核心功能 SecKill接口 /seckill?product=20&source=android& ...

  7. 雷林鹏分享:jQuery EasyUI 数据网格 - 自定义分页

    jQuery EasyUI 数据网格 - 自定义分页 数据网格(datagrid)内置一个很好特性的分页功能,自定义也相当简单.在本教程中,我们将创建一个数据网格(datagrid),并在分页工具栏上 ...

  8. Spring MVC 返回Json数据环境记录

    Spring 版本 Spring4.3.18 Json包  jackson-annotations-2.9.8.jar jackson-core-2.9.8.jar           jackson ...

  9. Python方法和属性的动态绑定 --面向对象

    需要了解的: __ slots __:属于魔术变量,即:系统实现某些特定功能的变量 __ slots __: 1.限定类实例能添加的属性 2.对当前类起作用,对继承的子类是不起作用的 实操: # co ...

  10. LeetCode--031--下一个排列(java)*

    实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常数空间. ...