字符串查找 · Implement strStr()
[抄题]:
对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1。
如果 source = "source" 和 target = "target",返回 -1。
如果 source = "abcdabcdefg" 和 target = "bcd",返回 1。
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
自己知道大概什么意思,但是不敢写。下次要进入写代码阶段
[一句话思路]:
- 双重for时,用i+j和j 比较,从而找到符合条件的i,头一次见
- for循环在不知道上限的时候也可以不写,头一次见
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 先找到起点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()的更多相关文章
- php中常用的字符串查找函数strstr()、strpos()实例解释
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...
- 字符串函数(strcpy字符串拷,strcmp字符串比较,strstr字符串查找,strDelChar字符串删除字符,strrev字符串反序,memmove拷贝内存块,strlen字符串长度)
1.strcpy字符串拷贝拷贝pStrSource到pStrDest,并返回pStrDest地址(源和目标位置重叠情况除外) char *strcpy(char *pStrDest, const ch ...
- LeetCode OJ:Implement strStr()(实现子字符串查找)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- lintcode:strStr 字符串查找
题目: 字符串查找 字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数.你的任务是实现这个函数. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source ...
- 28. Implement strStr()(KMP字符串匹配算法)
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [leetcode] 21. Implement strStr()
这个题目是典型的KMP算法,当然也可以试试BM,当然有关KMP和BM的介绍阮一峰曾经写过比较好的科普,然后july也有讲解,不过那个太长了. 先放题目吧: Implement strStr(). Re ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- centos6.6升级安装MySQL5.5(2015/3/4)
使用系统CentOS 6.6本来已经系统自带安装了mysql 5.1,但是奈何5.1不支持utf8mb4字符集(详见:http://blog.csdn.net/shootyou/article/det ...
- mysql用触发器同步表
一.先复制表 : and DATE = '2016-09-26' or DATE = '2016-09-27'; 二.创建插入数据时的[触发器] [在phpmyadmin 运行时记得要修改语句定界符 ...
- WebApi和Andriod对接访问模式问题
最近在做WebApi和Andriod接口的对接,中途出现一个问题就是返回格式的问题.由于之前使用WebService的时候使用的一直都是json的序列化和反序列话格式,所以一开始在webapi中通样使 ...
- year()+month() 不错的Idear
year(发货日期)*100+month(发货日期),可以取到年份+月份(月份不到10月的,自动补0)
- Hibernate学习7—Hibernate 映射继承
需求:学生有很多照片,分为生活照和工作照: 第一节:每个具体类对应一个表 Student.java: package com.cy.model; import java.util.Set; publi ...
- PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- AbstractQueuedSynchronizer原理分析
AbstractQueuedSynchronized 以下简称AQS,是用来构建锁或者其他同步组件的基础框架. 在AQS中,为锁的获取和释放提供了一些模板方法,而实现锁的类(AQS的子类)需要实现这些 ...
- 常见的sql server 链接问题------持续更新
问题1:超时时间已到.超时时间已到,但是尚未从池中获取连接.出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小 再查询窗口输入exec sp_who2进行查询链接消耗资源 可能出现的情况是 ...
- Understanding OpenStack Authentication: Keystone PKI
The latest stable release of OpenStack, codenamed Grizzly, revolutionizes the way user authenticatio ...
- Java复习——反射和泛型的复习
反射 Class类 一个类被类加载器加载到内存之中,占有一片区域,这个空间里的内容就是类的字节码,不同的类的字节码是不一样的,这一个个空间页可以使用类来表示,这就是Class类. 根据这个概念可知:不 ...