实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。

示例 1:

输入: haystack = "hello", needle = "ll"
输出: 2
示例 2:

输入: haystack = "aaaaa", needle = "bba"
输出: -1
说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

来源:力扣(LeetCode)

class Solution {
    /**
     * @param String $haystack
     * @param String $needle
     * @return Integer
     */
    function strStr($haystack, $needle) {
        if((empty($haystack) && empty($needle)) || empty($needle)) return 0;
        $key = strpos($haystack,$needle);
       return $key === false ? -1 : $key;
    }
}
 
第二种
 

class Solution {

/**
* @param String $haystack
* @param String $needle
* @return Integer
*/
function strStr($haystack, $needle) {
$str1 = strlen($haystack);
$str2 = strlen($needle);

if($needle === '') return 0;

if($str2 > $str1){
return -1;
}

for($i = 0; $i < $str1;$i++){

if($haystack{$i} === $needle{0}){

$strleng = substr($haystack,$i,$str2);

if($strleng === $needle){
return $i;
}

}

}
return -1;
}
}

 

PHP - 实现 strStr()的更多相关文章

  1. [PHP源码阅读]strpos、strstr和stripos、stristr函数

    我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...

  2. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. strstr 函数的实现

    strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...

  4. 28. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  5. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. LintCode StrStr

    1. 讨论目标字符串若为空, 则返回-1: 资源字符串若为空, 则返回-1. 2.讨论目标字符串个数为零, 则返回0: 资源字符串个数为零, 则返回-1. 3. 插入旗帜来使第二循环的结束为有条件地返 ...

  7. strstr函数

    原型:char * strstr( char *haystack,  char *needle ) 用法:#include <string.h> 功能:在haystack中寻找needle ...

  8. strstr函数的用法

    C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型:      extern char *strstr(char *str1, const char *str2); 语法: ...

  9. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  10. C语言(函数)学习之strstr strcasestr

    C语言(函数)学习之[strstr]&[strcasestr]一.strstr函数使用[1]函数原型char*strstr(constchar*haystack,constchar*needl ...

随机推荐

  1. 将arcEngine9.3和dev9.2.4开发的项目升级成arcObject10.2和dev15.1.3过程中遇到的问题和解决

    好久没碰.net了,arcgis更是感觉都忘干净了,今天将arcEngine9.3和dev9.2.4开发的一个项目升级成arcObject10.2和dev15.1.3过程中遇到了一系问题,留个笔记,留 ...

  2. 请问如何实现字符串UTF8->BIG5,BIG5->UTF8。保证送分。-Java/JavaSE

    请问如何实现字符串UTF8-> BIG5,BIG5-> UTF8. ------回答--------- ------其他回答(100分)--------- public String BI ...

  3. C# Copy一个文件到另一个文件夹下

    public static void CopyToFile() { //源文件路径 string sourceName = @"D:\Source\Test.txt"; //目标路 ...

  4. 配置文件--spring cloud Config

    配置中心--Spring cloud Config 通过本次学习,我们应该掌握: Config Server 读取配置文 Config Server 从远程 Git 仓库读取配置文 搭建芮可用 Con ...

  5. vue全家桶(vue2.x+vue-router+axios+webpack)项目搭建

    参考博客文章 博主FungLeo的Vue2+VueRouter2+Webpack+Axios 构建项目实战2017重制版 注:原博主写的非常详细 本文章为根据原博主教程总结的自己的搭建流程 一.安装n ...

  6. 调试Android有什么错误

    项目目录的cmd调试查看有什么错误 gradlew processDebugManifest --stacktrace

  7. C/C++ volatile

    { volatile和const关键很相似,都是修饰变量的,只是二者功能不一样. volatile在多线程当中经常使用,因为在某一线程多次调用某一个变量,编译器会进行优化,将该变量存放在在寄存器当中, ...

  8. 8、iota枚举

    1.iota常量自动生成器,每一行,自动累加1 2.iota给常量赋值使用 3.如果iota遇到const,就会重置为0 4.可以可以只写一个iota 5.如果是同一行,值是一样的 // 09_iot ...

  9. Robot Framework:随机数

    脚本 随机数 # 随机生成几位随机数 ${num} set variable 6 ${random} evaluate "".join(random.sample(string.l ...

  10. php-异步上传插件

    http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html