[抄题]:

对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1

如果 source = "source" 和 target = "target",返回 -1

如果 source = "abcdabcdefg" 和 target = "bcd",返回 1

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

自己知道大概什么意思,但是不敢写。下次要进入写代码阶段

[一句话思路]:

  1. 双重for时,用i+j和j 比较,从而找到符合条件的i,头一次见
  2. for循环在不知道上限的时候也可以不写,头一次见

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. 先找到起点i,确认可以走。然后讨论j 走完、没走完两种情况。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

for (int i = 0; ; i++) {
for (int j = 0; ; j++) {
if (j == needle.length()) return i;//j finished
if (i+j == haystack.length()) return -1;//i finished but j not finished
if (haystack.charAt(i + j) != needle.charAt(j)) break;

charAt(i + j) != charAt(j)

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int strStr(String haystack, String needle) {
//corner case
if (needle == null || haystack == null) {
return -1;
}
//find, not find, not equal
for (int i = 0; ; i++) {
for (int j = 0; ; j++) {
if (j == needle.length()) return i;//j finished
if (i+j == haystack.length()) return -1;//i finished but j not finished
if (haystack.charAt(i + j) != needle.charAt(j)) break;
}
}
}
}

字符串查找 · Implement strStr()的更多相关文章

  1. php中常用的字符串查找函数strstr()、strpos()实例解释

    string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...

  2. 字符串函数(strcpy字符串拷,strcmp字符串比较,strstr字符串查找,strDelChar字符串删除字符,strrev字符串反序,memmove拷贝内存块,strlen字符串长度)

    1.strcpy字符串拷贝拷贝pStrSource到pStrDest,并返回pStrDest地址(源和目标位置重叠情况除外) char *strcpy(char *pStrDest, const ch ...

  3. LeetCode OJ:Implement strStr()(实现子字符串查找)

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

  4. lintcode:strStr 字符串查找

    题目: 字符串查找 字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数.你的任务是实现这个函数. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source ...

  5. 28. Implement strStr()(KMP字符串匹配算法)

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

  6. [leetcode] 21. Implement strStr()

    这个题目是典型的KMP算法,当然也可以试试BM,当然有关KMP和BM的介绍阮一峰曾经写过比较好的科普,然后july也有讲解,不过那个太长了. 先放题目吧: Implement strStr(). Re ...

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

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

  8. Leetcode 详解(Implement strstr)

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

  9. Implement strStr()

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

随机推荐

  1. 【转】VC 模式对话框和非模式对话框的创建,销毁和区别

    原文网址:http://blog.csdn.net/mycaibo/article/details/6002151 VC 模式对话框和非模式对话框的创建,销毁和区别   在WIN32中,模式对话框的创 ...

  2. qing-automation简单入门介绍

    1.相关文档:http://www.51testing.com/html/50/category-catid-250.html 2.进行Qing automation相关操作之前,必须安装好jdk跟a ...

  3. 【经验】实现STL算法时遇到的模板编译错误问题

    在实现set_union算法时调用了自己写的copy算法,出现了以下问题. Error 1 error C2665: 'xyz_stl::__copy' : none of the 2 overloa ...

  4. Spring MVC中发布Restful Web服务

      对于企业应用来说,数据是许多业务的命脉,软件通常是可替换的,但是多年积累的数据是永远不能替换的.   近些年来,以信息为中心的表述性状态转移(Representational State Tran ...

  5. java基础-构建命令行运行的java程序简要注意

    今天编写了一个运行在服务端的java工具类,才发现自己以前很少关注运营方面的内容,导致在服务端部署一个java的工具变得异常困难,其实这也是自己对java的了解不够造成的. 首先,当代码编写完成之后, ...

  6. Whoops, looks like something went wrong

    Whoops, looks like something went wrong. 这是由于访问laravel项目报错的,解决几种可能出现的错误. 1)打开:D:\java\wamp\www\subwa ...

  7. CocoStudio资源区导入Plist/PSD文件

    这两种文件在使用中和普通文件稍有不同,下作简单介绍.如有不适的地方欢迎批评指正. 首先简单说一下Plist文件,Plist文件通常用于储存用户设置,也可以用于存储捆绑的信息,该功能在旧式的Mac OS ...

  8. zabbix agent主动模式与proxy模式,实现公网zabbix监控私网客户机

    zabbix agent主动模式,实现公网zabbix监控私网客户机 zabbix_server端当主机数量过多的时候,由Server端去收集数据,Zabbix会出现严重的性能问题,主要表现如下:  ...

  9. swoole学习

    <?php ini_set('default_socket_timeout', -1); class serverEmail { public $serv = null; public func ...

  10. php中$_POST接收不到参数问题

    问题描述:PHP可以接收_GET._SERVER._COOKIE等参数,php://input可以读取没有处理过的POST数据,独独_POST接收不到post参数.  原因: php://input可 ...