LeetCode 028 Implement strStr()
题目要求:Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Java:
public int strStr(String haystack, String needle) {
if (haystack == null || needle == null) {
return 0;
}
if (needle.length() == 0) {
return 0;
}
for (int i = 0; i < haystack.length(); i++) {
if (i + needle.length() > haystack.length()) {
return -1;
}
int m = i;
for (int j = 0; j < needle.length(); j++) {
if (needle.charAt(j) == haystack.charAt(m)) {
if (j == needle.length() - 1) {
return i;
}
m++;
} else {
break;
}
}
}
return -1;
}
LeetCode 028 Implement strStr()的更多相关文章
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【LeetCode】028. Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [leetcode 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- 【leetcode】Implement strStr() (easy)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【leetcode】Implement strStr()
Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- #10053 L 语言
L 语言 dalao 看来是水题?我可不这么认为. 很多人都写了我认为不怎么正确的贪心,那就是直接看到一个单词就减去. 那么这组数据就可以 hack 掉了: 2 1 whatis what whati ...
- How to get last SysExcelWorksheet object row or column[X++]
findLastColumn int findLastColumn(SysExcelWorksheet _sysExcelWorksheet, boolean _data = true) { #Exc ...
- CSS三大特性及权重叠加
层叠性: 1.样式冲突,遵循的原则是就近原则,哪个样式离结构近,就执行哪个样式 2.样式不冲突,不会层叠 继承性: 子标签会继承父标签的某些样式,如文本颜色和字号 优先级: 当同一个元素指定多个选择器 ...
- Doctrine\ORM\QueryBuilder 源码解析之 where
背景 最近有需求实现类似于 QueryBuilder 的谓词语句,就去翻看了它的源码.先看两个例子 例子1 $qb = $em->createQueryBuilder(); $qb->se ...
- JS多物体宽度运动案例
任务 对于每一个Div区块,鼠标移入,宽度逐渐变宽,最宽值为400px,当鼠标移除时,宽度逐渐减小,最小值为100px. 任务提示: (1)多物体运动的定时器需要需要每个物体上同时最多只能开一个定时器 ...
- subString引起的index out of range
特别注意!!!低级坑 subString(begin,end) subList()均存在这个问题. 当end>String.size(),则index out of range!!!
- awk1
awk [选项参数] 'script' var=value file(s)或aawk [选项参数] -f scriptfile var=value file(s)项参数说明:-F fs or --fi ...
- 与pandas初相识
前一阵子有个同事说,他看不懂从kibana上拉下来的日志,但是又想分析一些数据,感觉很头痛,每次都找开发给他整理一下,但是开发也很忙,要数据的频率也略高,那时候正好我跟这位需求方的项目,负责测试工作. ...
- ipmi常用的命令行命令
前言 记录一些常用的命令行操作 命令 查询机器的电源状态 ipmitool -I lanplus -U admin -P admin -H 172.16.21.215 power status 硬重启 ...
- mds/journal.cc: 2929: FAILED assert解决
前言 在处理一个其他双活MDS无法启动环境的时候,查看mds的日志看到了这个错误mds/journal.cc: 2929: FAILED assert(mds->sessionmap.get_v ...