28. Implement strStr()【easy】
28. Implement strStr()【easy】
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
解法一:
class Solution {
public:
int strStr(string haystack, string needle) {
if (haystack.empty() && needle.empty()) {
return ;
} if (haystack.empty()) {
return -;
} if (haystack.size() < needle.size()) {
return -;
} for (string::size_type i = ; i < haystack.size() - needle.size() + ; i++) {
string::size_type j = ;
for (j = ; j < needle.size(); j++) {
if (haystack[i + j] != needle[j]) {
break;
}
} if (j == needle.size()) {
return i;
}
} return -;
}
};
28. Implement strStr()【easy】的更多相关文章
- 28.Implement strStr()【leetcod】
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 495. Implement Stack【easy】
Implement a stack. You can use any data structure inside a stack except stack itself to implement it ...
- c语言 Implement strStr()【Leetcode】
实现在一个母字符串中找到第一个子字符串的位置. #include <stdio.h> #include <string.h> #define _IRON_TRUE 1 #def ...
- 28. Search a 2D Matrix 【easy】
28. Search a 2D Matrix [easy] Write an efficient algorithm that searches for a value in an mx n matr ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 141. Sqrt(x) 【easy】
141. Sqrt(x) [easy] Implement int sqrt(int x). Compute and return the square root of x. Example sqrt ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
随机推荐
- 【分类讨论】bzoj3856 Monster
#include<cstdio> using namespace std; int T=0; long long h,a,b,k; int main() { freopen("b ...
- 【莫比乌斯反演+分块】BZOJ1101-[POI2007]Zap
[题目大意] 对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d. [思路] 前面的思路同HDU1695 不过不同的是这道题中(a,b)和(b ...
- 软件配置篇-MySQL下载及安装
1.进入MySQL官网下载 下载地址:https://dev.mysql.com/downloads/mysql/ 选择合适的版本下载. 2.安装MySQL 解压后文件夹一般名为:mysql-x.x. ...
- Java高级架构师(一)第10节:Spring+Mybatis实现DAO
maven配置memcached.jar 由于目前java memcached client没有官方的maven repository可供使用,因此使用时需要手动将其安装到本地repository. ...
- SQL server 2008 安装问题解决 转
http://www.cnblogs.com/Hackerman/p/4472811.html 安装sqlserver2008 出现的一些问题解决方法 1,安装sqlserver的时候出现如下图所 ...
- 【jQuery】jquery中 使用$('#parentUid').attr(parentUid);报错jquery-1.11.3.min.js:5 Uncaught TypeError: Cannot read property 'nodeType' of undefined
jquery中 使用$('#parentUid').attr(parentUid);报错jquery-1.11.3.min.js:5 Uncaught TypeError: Cannot read p ...
- C#调用页面中的窗体中的方法,获取窗体的元素。
页面中的窗体 <div class="div_width" style="width: 100%; height: 95%;"> <ifram ...
- Node.js 连接mySQL程序
环境:Oracle Enterprise Linux R5U7 安装mySQL 关于离线安装,下次在尝试,目前先来在线安装,过程如下: $ rpm -qa | grep -i mysql $ wget ...
- OneThink框架的文章详情页分页
Application/Home/Controller/ArticleController.class.php的detail函数修改结果如下: /* 文档模型详情页 */public function ...
- flask的配置设置的几种方式
Flask的配置对象(config)是一个字典(dict)的子类(subclass),所以你可以把配置用键值对的方式存储进去. 1.一些重要的配置,可以设置在系统环境变量里,又或者放到某个服务器里, ...