实现 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. pytest_参数化3

    import pytesttest_user_data=[ {'user':'linda','password':'8888'}, {'user':'servenruby','password':'1 ...

  2. Centos7上MariaDB数据库启动问题解决

    安装MariaDB数据库后出现服务启动失败问题, 解决办法:卸载再安装!(确定无3306端口占用) 一.卸载数据库: [root@localhost logs]# yum -y remove mari ...

  3. Linux 任务管理器(二)

    特殊文件与进程 fuser命令 [root@localhost home]# fuser -muv . 用户 进程号 权限 命令 /home: root kernel mount (root)/hom ...

  4. 使用ReadStream方法延时读取文件

    const fs = require('fs'); let file = fs.createReadStream("filenpath.js"); file.pause(); fi ...

  5. mybatis group by查询返回map类型

    故事的发生是这样的. . . . . . . 一天 我发现我们的页面显示了这样的汇总统计数据,看起来体验还不错哦-- 然后,我发现代码是这样滴:分开每个状态分别去查询数量. 额e,可是为嘛不使用简单便 ...

  6. 【leetcode】bash脚本练习

    [192]Word Frequency Write a bash script to calculate the frequency of each word in a text file words ...

  7. 40th 要掀桌子么 还是尬坐吧

    今日学习精华:     面向对象编程里面有一句  非常经典的描述:-----通过类实例化一个对象,通过对象调方法-----   注意:对象调用的  方法 ,即 函数一定要有  参数      def  ...

  8. zabbix之自动注册,编写触发器:一定时间内超过某个负载值的时间

    zabbix中添加主机很多时可以考虑自动注册来自动添加,下面按照图片顺序来看,zabbix版本:3.0.28 超过负载30%的持续时间 创建触发器:

  9. git上传本地项目带Github上

    创建好线上版本库以后就可以在本地进行上传 1.选择好文件夹右击Git Bash Here 2.先创建本地版本库 git init 3.git add README.md 4.git commit -m ...

  10. stl+数论——1247D

    其实也不算很难想,每个元素质因子分解后的p^c的p和c用pair的形式存在每个元素vector里 要去前面找一个数使得所有指数相加是k的倍数,那么把vector里的所有c 模 k,然后去找前面互补的数 ...