LeetCode28 Implement strStr()
题目:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. (Easy)
分析:
不用考虑KMP啥的,就是写好暴力写法就行。
两重循环,注意空串判定,注意haystack比needle长,注意外层循环的终止条件。
代码:
class Solution {
public:
int strStr(string haystack, string needle) {
if (haystack.size() == && needle.size() == ) {
return ;
}
if (haystack.size() < needle.size()) {
return -;
}
for (int i = ; i < haystack.size() - needle.size() + ; ++i) {
int flag = ;
for (int j = ; j < needle.size(); ++j) {
if (haystack[i + j] != needle[j]) {
flag = ;
break;
}
}
if (flag == ) {
return i;
}
}
return -;
}
};
LeetCode28 Implement strStr()的更多相关文章
- [Swift]LeetCode28. 实现strStr() | Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 28. Implement 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 ...
- [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(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- asp.net mvc 4 编译错误 CS0012
CS0012: The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is no ...
- C++11多元组类别
[C++11多元组类别] 多元组可被视为是 struct 其数据成员的一般化.底下是一个多元组类别的定义和使用情况: 我们可以定义一个多元组类别对象 proof 而不指定其内容,前提是 proof 里 ...
- Labview中的属性节点
获取(读取)和/或设置(写入)引用的属性.通过属性节点对本地或远程应用程序实例.VI或对象获取或设置属性和方法也可通过属性节点访问LabVIEW类的私有数据. 属性节点可自动调整为用户所引用的对象的类 ...
- apache配置虚拟主机后,启动速度慢
apache配置虚拟主机后,启动速度慢且提示“the requested operation has failed” 可以通过在cmd下启动,来查找问题(命令中的“apache2.2”,是服务名,根据 ...
- JQ避免出现多次执行一个事件的解决方案
点击按钮之后会多次执行一个事件的话,就在方法结尾加入如下代码,这样的话事件就可以只执行一次了 //避免出现多次执行事件的问题 event.stopPropagation(); 此外,时间的重复绑定也有 ...
- Kafka架构设计:分布式发布订阅消息系统
[http://www.oschina.net/translate/kafka-design](较长:很详细的讲解) [我们为什么要搭建该系统]用作LinkedIn的活动流(activity stre ...
- Android学习过程中遇到的问题
1.使用在Activity布局之上重叠显示操作栏,第一次使用出现错误信息. 错误信息如下:java.lang.RuntimeException:Ubable to start activity Com ...
- opennebula 出错截图与调试
- mac 开发必备软件(不断update ing...)
整理下mac环境下, 开发必备的一些软件吧, 由于不断要更新ing, 用到啥就写啥~球轻拍 1.host 绑定切换神器 a.gas mask : 只能切换单个自定义的host文件 b.ihosts(推 ...
- 重载PostNcDestroy()函数做一些清理工作
转自:http://zhidao.baidu.com/link?url=W-OOWnvUx58w1esKfRAMtvbzBhjfyKodhk8j2DcTtlcDKAOy8sBNW-8Ey2RAhc0l ...