字符串查找 · 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 ...
随机推荐
- 虚拟主机wordpress文件上传大小限制更改
默认的wp文件上传的大小都是2M 登录阿里云进入控制面板找到你的虚拟机实例 点击管理 改成10M,最大也就是10,虚拟机的睾丸之处.保存,去页面新媒体添加可以看到最大限制为10M了
- linux-docker安装
https://yeasy.gitbooks.io/docker_practice/content/install/centos.html
- p2p通信原理及实现(转)
1.简介 当今互联网到处存在着一些中间件(MIddleBoxes),如NAT和防火墙,导致两个(不在同一内网)中的客户端无法直接通信.这些问题即便是到了IPV6时代也会存在,因为即使不需要NAT,但还 ...
- list.ForEach的用法
Templist.ForEach(o => { var isSel = ReviewerFileRelationService.Where(s => s.PackageFileId == ...
- RK3288 添加USB转虚拟串口设备
在系统开启并有日志打印的前提下,插入USB设备,就会打印USB设备和虚拟串口信息. 打印信息如下: 供应商ID(VID):idVendor=1234,产品ID(PID): idProduct=5678 ...
- RK3288 手动设置电池电量
参考:[RK3288][Android6.0] 调试笔记 --- 电池电量一直显示100% 系统版本:RK3288 android 5.1 (与参考的变量和宏有点区别) 设备没有电池,在进行Fota升 ...
- Windows 键盘快捷键概述
在 Windows 中工作时,利用快捷键代替鼠标.可以利用键盘快捷键打开.关闭和导航“开始”菜单.桌面.菜单.对话框以及网页.键盘还可以让您更简单地与计算机交互. 单击一个标题或按 TAB 键可以突 ...
- java后台读取配置文件中key与value -----demo2
/** * * @Title: getValue * @Description: TODO * @param key * @return import java.util.Properties; * ...
- Julia - 函数返回值
return 返回值 要返回函数最后一个表达式的值,可以省略 return julia> function f(x, y) x + y end f (generic function with ...
- 读《分布式一致性原理》CURATOR客户端3
分布式锁 在分布式环境中,为了保证数据的一致性,经常在程序运行的某个运行点.需要进行同步控制. package master; import java.text.SimpleDateFormat; i ...