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. 【动态规划】bzoj1633 [Usaco2007 Feb]The Cow Lexicon 牛的词典

    f[i]=min{f[i+1]+1,f[i+len[j]+cant]+cant}(for i=L-1 downto 0)(1<=j<=w) #include<cstdio> # ...

  2. [CF915F]Imbalance Value of a Tree

    [CF915F]Imbalance Value of a Tree 题目大意: 一棵\(n(n\le10^6)\)个结点的树,每个结点有一个权值\(w_i\).定义\(I(i,j)\)为\(i\)到\ ...

  3. IDEA ULTIMATE 2019.1 注册码,亲测可用

    在 hosts 文件里加入如下的配置: 0.0.0.0 account.jetbrains.com 0.0.0.0 www.jetbrains.com # 2019.1得加这个 注册码: N757JE ...

  4. js时间小总结

    1.js获取时间 var myDate = new Date(); 1 myDate.getYear(); //获取当前年份(2位) 2 myDate.getFullYear(); //获取完整的年份 ...

  5. activiti-explorer 流程中文乱码问题

    1.activiti-cfg.xml的修改,添加activityFontName和labelFontName属性 <bean id="processEngineConfiguratio ...

  6. 2015年Ubuntu最新Redmine的安装和配置

    近期须要在公司内部搭建一个项目管理平台Redmine,在摸索了一天之后.最终配置成功,在这里分享给大家. 公司server的系统是Ubuntu14.04,要安装的是最新的Redmine3.0. 因为R ...

  7. ibatis传入list对象

    在使用ibatis的时候经常需要传入list对象,sql语句如下. <select id="GET-PERSONS" parameterClass="java.ut ...

  8. 扩展 jQuery EasyUI Datagrid 数据行鼠标悬停/离开事件(onMouseOver/onMouseOut)

    客户需求: jQuery EasyUI Datagrid 用户列表鼠标悬停/离开数据行时显示人员头像(onMouseOver/onMouseOut) 如图所示,Datagrid 鼠标悬停/离开数据行时 ...

  9. linux设置定制器自动执行任务

    基本格式 :  * * * * * command  分 时 日 月 周 命令  第1列表示分钟1-59 每分钟用*或者 */1表示  第2列表示小时1-23(0表示0点)  第3列表示日期1-31  ...

  10. NDK官方开发指南翻译之 NDK_GDB

    这几天看JNI,没有基础,那真是难受--把看到的相关资料记录一下,也分享给刚開始学习的人. 'ndk-gdb' Overview 重要:假设你要调试线程相关的程序.请阅读以下的'Thread Supp ...