题目:

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()的更多相关文章

  1. [Swift]LeetCode28. 实现strStr() | Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. 28. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  5. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  6. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  7. 【leetcode】Implement strStr() (easy)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  8. [LeetCode] Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  9. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

随机推荐

  1. [Cocos2d-JS] 安卓机器的几个按钮

    cc.eventManager.addListener({ event:cc.EventListener.KEYBOARD, onKeyPressed:function(keycode,event){ ...

  2. dom 侧栏分享

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 面试题收集——Java基础部分(一)

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/3641769.html 感谢! 1.一个".java"源文件中是否可以包括多个类(不是内部类) ...

  4. <转载>linux下内存泄露查找、BUG调试

    先收藏着,抽空好好看看:http://www.ibm.com/developerworks/cn/linux/l-pow-debug/ 简介 调试程序有很多方法,例如向屏幕上打印消息,使用调试器,或者 ...

  5. hdu 4289 Control(最小割 + 拆点)

    http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  6. Mac 与 PC 键盘布局对比

  7. LeetCode292:Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  8. oracle常见小问题解答ORA-01008,ORA-01036

    第一个问题,参数传的空值,需要检查参数们有没有空值的情况 第二个问题,与MSSQL不同的是,.net使用参数化调用oracle不加@加的是:,然后在参数化语句里面可以省略:冒号,如果不这么写,就会出现 ...

  9. 100个直接可以拿来用的JavaScript实用功能代码片段(转载)

    把平时网站上常用的一些实用功能代码片段通通收集起来,方面网友们学习使用,利用好的话可以加快网友们的开发速度,提高工作效率. 目录如下: 1.原生JavaScript实现字符串长度截取2.原生JavaS ...

  10. Spring REST实践之客户端和测试

    RestTemplate 可参考spring实战来写这部分. RestTemplate免于编写乏味的样板代码,RestTemplate定义了33个与REST资源交互的方法,涵盖了HTTP动作的各种形式 ...