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

  1. 28.Implement strStr()【leetcod】

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

  2. 495. Implement Stack【easy】

    Implement a stack. You can use any data structure inside a stack except stack itself to implement it ...

  3. c语言 Implement strStr()【Leetcode】

    实现在一个母字符串中找到第一个子字符串的位置. #include <stdio.h> #include <string.h> #define _IRON_TRUE 1 #def ...

  4. 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 ...

  5. 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 ...

  6. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  7. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  8. 141. Sqrt(x) 【easy】

    141. Sqrt(x) [easy] Implement int sqrt(int x). Compute and return the square root of x. Example sqrt ...

  9. leetCode练题——28. Implement strStr()

    1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...

随机推荐

  1. AOJ 2230 How to Create a Good Game(费用流)

    [题目链接] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2230 [题目大意] 给出一张图,从1到n的最长路不变的情况下, 还能 ...

  2. 【分块】bzoj1593 [Usaco2008 Feb]Hotel 旅馆

    分块,记录每个块内包括左端点的最大连续白段的长度, 整个块内的最大连续白段的长度, 和包括右端点的最大连续白段的长度. Because 是区间染色,所以要打标记. 至于怎样在O(sqrt(n))的时间 ...

  3. 【块状树】【博弈论】bzoj3729 Gty的游戏

    块状树,每个块的根记录一下当前块内距块根为奇数距离的异或和和偶数距离的异或和,询问的时候讨论一下即可. 总的节点数可能超过50000. #include<cstdio> #include& ...

  4. [CF160D]Edges in MST

    [CF160D]Edges in MST 题目大意: 一个\(n(n\le10^5)\)个点,\(m(m\le10^5)\)条边的连通图.对于图中的每条边,判断它与该图最小生成树的关系: 在该图所有的 ...

  5. 上传--下载HDFS文件并指定文件物理块的大小

    使用hdfs的api接口分别实现从本地上传文件到集群和从集群下载文件到本地. 1)上传文件主要是使用FileSystem类的copyFromLocalFile()方法来实现,另外我们上传文件时可以指定 ...

  6. ubuntu使用ssh远程登录服务器及上传本地文件到服务器

    1. ubuntu 远程登录   首先你的ubuntu要能够支持ssh,如果不能,自行百度! 打开终端,输入 ssh  root@115.159.200.13(你的服务器的IP地址) 回车就会让你输入 ...

  7. TCP Socket一些东西

    1. 若connect失败该套接字不可再用,必须close当前套接字,重新调用socket. 手册上注明连接失败后, socket的状态是未知的, 所以再次connect, 可能成功, 也可能失败. ...

  8. mysql group_concat函数

    函数语法: group_concat( [DISTINCT] 要连接的字段 [Order BY 排序字段 ASC/DESC] [Separator '分隔符'] ) 下面举例说明: select * ...

  9. Oracle导入本属于sys用户的表

    FlashBack Database后,将删除的数据导出时使用了system用户 exp system/oracle file=/home/oracle/test.dmp tables=sys.tes ...

  10. Centos:mysql的安装和使用:yum方式

    1.安装: 安装客户端 sudo yum install mysql 安装服务器 sudo yum install mysql-server 2.配置:查看配置文件 cat /etc/my.cnf 3 ...