[抄题]:

对于一个给定的 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. 【转】python 字符编码与解码——unicode、str和中文:UnicodeDecodeError: 'ascii' codec can't decode

    原文网址:http://blog.csdn.net/trochiluses/article/details/16825269 摘要:在进行python脚本的编写时,如果我们用python来处理网页数据 ...

  2. angular的路由跳转,的监听$rootScope.$on

    使用angular来做项目时,习惯性的使用第三方路由插件ui-router配置路由.每一个状态都对应着一个页面, 因此对路由状态改变的监听也变的十分重要. 可以使用:$rootScope.$on(…… ...

  3. laravel里面的控制器笔记

    看了下教程,总结了下,大概分两种 一般的controller restful的controller 单独绑定action的route为 Route::get('user/{id}', 'UserCon ...

  4. FPGA能代替CPU架构吗?

    你还没听过FPGA?那你一定是好久没有更新自己在企业级IT领域的知识了.今天笔者就和大家聊聊何为FPGA?FPGA主要应用场景是什么?有人说FPGA是替代传统CPU和GPU的未来,你信吗? FPGA全 ...

  5. Linnx 服务器中mysql 无法正常访问问题

    本机连接远程Linnx服务器不通 1. 检测防火墙 -- 保证防火墙关闭 查看到iptables服务的当前状态:service iptables status. 但是即使服务运行了,防火墙也不一定起作 ...

  6. 杂项:HTML5-1/3-发展历程

    ylbtech-杂项:HTML5-1/3-发展历程 万维网的核心语言.标准通用标记语言下的一个应用超文本标记语言(HTML)的第五次重大修改(这是一项推荐标准.外语原文:W3C Recommendat ...

  7. 杂项: Redis

    ylbtech-杂项: Redis Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 1. 定义返回顶部 re ...

  8. 019:InnoDB 表空间内部组织结构

    一. 表空间内部组织结构 表空间 内部有多个段对象(Segment) 组成 每个段(Segment)由区(Extent) 组成 每个区(Extent)由页(Page)组成 每个页里面报存数据 (或者叫 ...

  9. 【洛谷】P2725 邮票 Stamps(dp)

    题目背景 给一组 N 枚邮票的面值集合(如,{1 分,3 分})和一个上限 K —— 表示信封上能够贴 K 张邮票.计算从 1 到 M 的最大连续可贴出的邮资. 题目描述 例如,假设有 1 分和 3 ...

  10. java.lang.ArrayIndexOutOfBoundsException异常分析及解决

    这是一个非常常见的异常,从名字上看是数组下标越界错误,解决方法就是查看为什么下标越界. 下面是一个错误示例: Exception in thread "main" java.lan ...