28.Implement strStr()【leetcod】
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack
public class Solution {
public int strStr(String haystack, String needle) {
if(haystack==null||needle==null)
return -1;
int lenHay =haystack.length();
int lenNeed=needle.length();
for(int i=0;i<=lenHay-lenNeed;i++){
int j=0;
for(j=0;j<lenNeed;j++){
if(haystack.charAt(i+j)!=needle.charAt(j)){
return -1;
// break;
}
}
if(j==lenNeed)
return i;
}
return -1;
}
}
解题思路:首先本题考查的为strStr()函数的实现
strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。
判断大字符串A和子串B,他们长度分别为L1和L2,这个时候i 在0-(L1-L2)区间内:记为可能出现的返回值。即子串的第j个字母在A串中的位置为i+j
如果最后子串的每个字母在母串中都有匹配到那么范围i的位置即可,否则证明不包含子串,返回-1即可
士不可以三日不读书,故士别三日当刮目相待!晚安,北京!
28.Implement strStr()【leetcod】的更多相关文章
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...
- c语言 Implement strStr()【Leetcode】
实现在一个母字符串中找到第一个子字符串的位置. #include <stdio.h> #include <string.h> #define _IRON_TRUE 1 #def ...
- [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 ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- 【LeetCode】28 - Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
- 【LeetCode】28. Implement strStr() (2 solutions)
Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...
- 【LeetCode】28. Implement strStr() 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...
随机推荐
- 搭建es6开发与非开发环境babel-browser
前言 最近打算把es6应用到项目中,但是如何在开发环境(浏览器端)直接运行es6?es6已经发布一段时间了,现在大部分是在node.js环境运行,或者通过babel编译之后运行.babel-brows ...
- 【Android Developers Training】 51. 序言:打印内容
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- form表单的ajax验证2
form表单的ajax验证2: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...
- 怎么使用CURL传输工具发送get或者post指令
1.先下载CURL,见网盘 2.使用,可以直接到doc,cd到curl.exe目录,然后执行 或者用脚本 Set exeRs = WshShell.Exec("curl.exe -F &qu ...
- 【项目1-1】使用HTML5+CSS3绘制HTML5的logo
作为一个WEB小萌新,自学了有一段时间,总是感觉停滞不前.最近反思中,想到前贤一句话:书读百遍其义自见.说到底,还是项目做的少,如果做多了,想必自然会得心应手. 利用HTML5+CSS3绘制HTML5 ...
- python实现希尔排序(已编程实现)
希尔排序: 观察一下”插入排序“:其实不难发现她有个缺点: 如果当数据是”5, 4, 3, 2, 1“的时候,此时我们将“无序块”中的记录插入到“有序块”时,估计俺们要崩盘, 每次插入都要移动位置,此 ...
- C# 文字转换最简单的方法
引用Microsoft.VisualBasic string text=Strings.StrConv("需要转换的文字", VbStrConv.TraditionalChines ...
- Python系列教程(一):简介
Python发展历史 起源 Python的作者,Guido von Rossum,荷兰人.1982年,Guido从阿姆斯特丹大学获得了数学和计算机硕士学位.然而,尽管他算得上是一位数学家,但他更加享受 ...
- Python 文件对象
Python 文件对象 1) 内置函数 open() 用于打开和创建文件对象 open(name,[,mode[,bufsize]]) 文件名.模式.缓冲区参数 mode: r 只读 w 写入 a 附 ...
- MQ队列管理
分享一段代码,很实用. 下面这段java代码是我在国外一个论坛上发现的,源地址已经忘了.代码的作用是可以删除正在使用的mq的队列消息,管理mq的人一定知道它的美妙了吧,哈哈. 我拿来改了下,增加了2个 ...