28. Implement strStr()【easy】
2024-08-29 14:03:50
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 -;
}
};
最新文章
- Vue - class与style绑定
- HTML5基础之textarea 和lable
- linux 下配置 nodejs+ionic+cordova
- js计算24点
- Elastic Search(一)
- 正斜杠和反斜杠-windows、web、c语言大讨论
- [改善Java代码]注意Class类的特殊性
- jQuery 全选 反选 单击行改变背景色
- bzoj1899
- 逃出克隆岛 (codevs 2059)
- typedef struct 是什么意思
- jsp 文件使用 include指令 导入 jspf 分析,及导入jspf 文件后出现乱码问题
- Redux-Saga学习心得
- 根据URL下载图片至客户端、服务器实例
- hdu 2044 递推
- MongoDB - 2
- .net正则匹配
- docker 基础之数据管理
- Potential Pythonic Pitfalls
- 算法训练 P1101