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】的更多相关文章

  1. 28. Implement strStr()【easy】

    28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...

  2. c语言 Implement strStr()【Leetcode】

    实现在一个母字符串中找到第一个子字符串的位置. #include <stdio.h> #include <string.h> #define _IRON_TRUE 1 #def ...

  3. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  4. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  5. leetCode练题——28. Implement strStr()

    1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...

  6. 【LeetCode】28 - Implement strStr()

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

  7. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

  8. 【LeetCode】28. Implement strStr() (2 solutions)

    Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...

  9. 【LeetCode】28. Implement strStr() 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...

随机推荐

  1. php获取当天的开始时间和结束时间戳

    $begin_time=date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y')));$over_time=dat ...

  2. crontab中引入环境变量(比如需要执行tomcat的关闭启动)

    起因 crontab中的定时任务,执行到关闭tomcat时,报环境变量找不到 解决方案 1.使用 . /etc/profile 引入环境变量 ###推荐, 实测ubuntu12 成功 2.使用 sou ...

  3. 简单说下Kanzi Studio

    一.Project 窗口 在Project窗口下可以创建界面节点,包含有Screen和Prefabs 二.Properties窗口 包含有节点的相关属性,不同类型的节点,属性不同.主要通过改变节点的属 ...

  4. SAP PI入门

    本教程的目的是让读者理解:SAP Process Intergration(以下简称SAP PI)是什么.我们不需要探究课题的本质,但是会讨论SAP PI的架构和不同特点.本文只会覆盖到PI的基本特点 ...

  5. Web自动化之Headless Chrome编码实战

    API 概览 && 编码Tips 文档地址 github Chrome DevTools Protocol 协议本身的仓库 有问题可以在这里提issue github debugger ...

  6. python webserver, based on SimpleHTTPServer

    #-*- coding:utf-8 -*- #author: lichmama #email: nextgodhand@163.com #filename: httpd.py import io im ...

  7. 在Linux安装配置Tomcat 并部署web应用 ( 三种方式 )

    系统版本:centos6.5版本 java版本:1.7 一.准备工作 1.java -version 检查是否有java环境,没有则需要去安装并配置到环境变量中. 2.下载tomcat包,下载地址:h ...

  8. 使用mysqldump备份数据库

    #! /bin/shday_str=`date +%j`day=`date +%Y%m%d`days_str=`echo "$day_str % 60"|bc`cd /home/d ...

  9. 第一篇:webservice初探

    接触webservice也有一段时间了,为了查缺补漏,把知识点系统化,准备写几篇博文梳理下webservice的知识点,这是第一篇,对webservice进行大致的介绍. 1.什么是webservic ...

  10. 【Java IO流】RandomAccessFile类的使用

    RandomAccessFile类的使用 RandomAccessFile类是java提供的对文件内容的访问,既可以读文件,也可以写文件. 支持随机访问文件,可以访问文件的任意位置. RandomAc ...