https://leetcode.com/problems/implement-strstr/

题目:

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

思路:

判断一个string是否另一个string的子序列并返回位置。

naive法:遍历查找,复杂度O(mn)。

advance法还有Rabin-Karp, KMP, Boyer- Moore algorithm等。

AC代码:

 class Solution {
public:
int strStr(string haystack, string needle) {\
int m=haystack.size();
int n=needle.size();
if(n== && m==)
return ;
bool flag=true;
for(int i=;i<m-n+;i++){
for(int j=;j<n;j++){
if(needle[j]!=haystack[i+j]){
flag=false;
break;
}
}
if(flag==true){
return i;
}
else
flag=true;
}
return -;
}
};

最新文章

  1. 网络数据包收发流程(三):e1000网卡和DMA
  2. LINQ to PostgreSQL Tutorial
  3. 2D游戏编程2--windows高级编程
  4. [!] Error installing AFNetworking
  5. (知识点)JS获取网页高度
  6. Strtus2框架笔记
  7. Begin again
  8. mac 卸载通过官网下载包安装的node
  9. linux 管道通信
  10. 纯小白创建第一个Node程序失败-容易忽略的一个细节
  11. 《从Paxos到ZooKeeper 分布式一致性原理与实践》读书笔记
  12. 如何用ABP框架快速完成项目(3) - 为什么要使用ABP和ABP框架简介
  13. 24.join算法/锁_1
  14. cmd关闭被占用的端口命令及教程详解
  15. 曲苑杂坛--DML操作中如何处理那些未提交的数据
  16. 动态规划刷题集python代码
  17. (算法)Binary Tree Max Path Sum
  18. 网络层-IP地址
  19. python学习笔记(二十九)为什么python的多线程不能利用多核CPU
  20. 阻塞队列之六:LinkedBlockingDeque

热门文章

  1. 在myeclipse中使用查找功能
  2. php 修改
  3. 用ajax异步请求一个块
  4. 九度oj 题目1090:路径打印
  5. TCP内核参数
  6. COdevs 2823 锁妖塔
  7. 【HDOJ6354】Everything Has Changed(计算几何)
  8. form:form的一点体会
  9. LeetCode OJ--Remove Duplicates from Sorted List II *
  10. Log4J使用详情