28. 实现 strStr()
d地址:https://leetcode-cn.com/problems/implement-strstr/
<?php /**
实现 strStr() 函数。 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。 示例 1: 输入: haystack = "hello", needle = "ll"
输出: 2
示例 2: 输入: haystack = "aaaaa", needle = "bba"
输出: -1
*/
class Solution { /**
1 将字符串的第n位与子串做对比
2 如果相等,子串下标+1,不相等,继续累加字符串下标 $i = $i- $j +1;
3 累加子串下标和字符串下标,判断下一个字串是否相等
4 如果子串下标等于子串长度,说明已经找到了,返回字符串下标-字串下标
*/
function strStr($haystack, $needle) {
if($needle == ''){return 0;} // 空字符串返回0
$len = strlen($haystack);
$length = strlen($needle); $i = 0;$j = 0;
while($i<$len && $j < $length){
echo $i.$j.'---';
if($haystack[$i] == $needle[$j]){
$j++;
$i++;
}else{
$i = $i- $j +1;
$j = 0;
}
if($j == $length){
return $i-$j;
}
}
return -1;
}
} $solution = new Solution();
$haystack = "mississippi";
$needle = "issip";
var_dump($solution->strStr($haystack,$needle));
28. 实现 strStr()的更多相关文章
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- 28.实现 strStr() 函数
28.实现 strStr() 函数 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在, ...
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...
- <每日 1 OJ> -LeetCode 28. 实现 strStr()
题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...
- 前端与算法 leetcode 28.实现 strStr()
# 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- Java实现 LeetCode 28 实现strStr()
28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...
- 【LeetCode】28. 实现 strStr()
28. 实现 strStr() 知识点:字符串:KMP算法 题目描述 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 ne ...
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
随机推荐
- 如何提升.NET控制台应用体验?
原文:Upgrade Your .NET Console App Experience 作者:Khalid Abuhakmeh 译文:Lamond Lu 在.NET生态系统中,控制台程序的表现相对较差 ...
- day1 执行用例
故事1 今天的工作,是执行已写好的"回馈"模块用例 每个子模块的用例都有优先级之分 肯定要先进行通过性测试,验证正常流 因此,我优先执行各个子模块优先级最高的那条用例,进行通过性测 ...
- 阿里AI芯片:12nm工艺、709平方毫米大核心
含光出自<列子·汤问>篇有“上古三剑”一章,寓意含而不露,光而不耀,象征含光 800 无形却强劲的算力. 含光 800 是一款 AI 芯片,偏重推理.据介绍,1 颗含光 800 的算力相当 ...
- 爬虫前奏——代理ip的使用
如果同一个IP短时见内多次访问统一网页,可能会被系统识别出是爬虫,因此使用代理IP可以很大程度上解决这一问题 常用的代理有: 西刺免费代理:www.xicidaili.com 快代理:www.kuai ...
- 机器学习 - LSTM应用之情感分析
1. 概述 在情感分析的应用领域,例如判断某一句话是positive或者是negative的案例中,咱们可以通过传统的standard neuro network来作为解决方案,但是传统的神经网络在应 ...
- [code]poj3349 Snowflake Snow Snowflakes
哈希+挂链.可以用next数组挂链. ; type arr=..]of longint; var a,b:Array[..]of arr; next:Array[..]of longint; i,j, ...
- python enumerate用法总结(转)
原文链接:https://blog.csdn.net/churximi/article/details/51648388 enumerate()说明 enumerate()是python的内置函数en ...
- [Linux][C][gcc] Linux GCC 编译链接 报错ex: ./libxxx.so: undefined reference to `shm_open'
本人原创文章,文章是在此代码github/note的基础上进行补充,转载请注明出处:https://github.com/dramalife/note. 以librt丶用户自定义动态库libxxx 和 ...
- c# winform 访问WebServices (通过Http方式)
第一步.编写WebServices服务方法 [WebMethod] public void PostJson(string str, string bb) { Dictionary<string ...
- scapy学习笔记
1.ACK Scan >>>ans,unans=sr(IP(dst="www.baidu.com")/TCP(dport=[80,666],flags=" ...