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 ...
随机推荐
- iOS打开手机QQ与指定用户聊天界面
开发中遇到一个联系客服qq的需求,找到这么一个实现方法,先记录下来.大概的原理就是,iOS启动第三方应用是采用schema模式的,这有点像url,打开不同的界面使用不同的地址.但这个url怎么得来的还 ...
- 未找到与约束contractname Microsoft.VisualStudio.Utilities.IContentTypeRegistryService
在項目中遇到的問題,網上找到的答案,做個記錄, 项目能打开,但是当要在项目中查看文件时弹出 未找到与约束 Microsoft.VisualStudio.Utilities.IContentTypeRe ...
- accelerated C++ 中查找url(学习笔记)
这个程序用来查找被称为url(统一资源地)的万维网地址,输入一行包含url的字符串,程序会帮你识别并输出字符串里所包含的全部url. url的格式: protocol-name(协议名称)://res ...
- hdoj 5387(Clock)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5387 比较水的一道题目,也是自己单翘的第一道题目吧,题意就是找到给定时间时钟三个指针之间的夹角, 需要 ...
- ELF学习--装载过程
当我们在linux bash下执行ELF程序时,Linux系统是怎样装载和执行的呢? 1.bash进程fork出子进程 2.在bash的子进程中调用execve系统调用来执行指定的ELF. 3.exe ...
- jquery自定义方法
总结: * jQuery中添加自定义或函数方法1,如 $.fn.extend({'aa':function(){}}) 或 jQuery.fn.aa=function(){}, 这种调用时就得这样,$ ...
- web前端—工作周报
2016.07.25-2016.07.29周报: 1.本周工作主要内容: A:完成了宏视云h5播放器升级及大数据上报: B:修复xk-h5播放器bug:在三星手机自带浏览器无法进行滑动seek; C ...
- Queue and Message
#ifndef __QUEUE_H__ #define __QUEUE_H__ #include <stdint.h> #include <stdlib.h> #include ...
- Microchip 125 kHz RFID System Design Guide
Passive RFID Basics - AN680 INTRODUCTION Radio Frequency Identification (RFID) systems use radio fre ...
- Cross-Browser HTML5 Placeholder Text
One of the nice enhancement in HTML5 web form is being able to add placeholder text to input fields. ...