解法一:Brute-force

 int strStr(string haystack, string needle)
{
int m = haystack.size();
int n = needle.size();
if (!n)
return ;
for (int i = ; i < m - n + ; ++i) {
int k = i, j = ;
while (j < n) {
if (needle[j] == haystack[k]) {
j++;
k++;
} else {
break;
}
}
if (j == n)
return i;
}
return -;
}

解法二:KMP

 vector<int> GetNext(const string& T)
{
int len = T.size();
vector<int> next(len, );
for (int i = , k = ; i < len;) {
if (T[i] == T[k])
next[i++] = ++k;
else if (k)
k = next[k - ];
else
next[i++] = ;
}
return next;
} int strStr(string haystack, string needle)
{
int m = haystack.size();
int n = needle.size();
if (n == )
return ; vector<int> next = GetNext(needle);
for (int i = , j = ; i < m;) {
if (haystack[i] == needle[j]) {
i++;
j++;
}
if (j == n)
return i - n;
if (i < m && haystack[i] != needle[j]) {
if (j)
j = next[j - ];
else
i++;
}
}
return -;
}

【LeetCode 28_字符串_匹配】Implement strStr()的更多相关文章

  1. 【Leetcode】【Easy】Implement strStr()

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

  2. 【leetcode刷题笔记】Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  3. 【LeetCode算法-28/35】Implement strStr()/Search Insert Position

    LeetCode第28题 Return the index of the first occurrence of needle in haystack, or -1 if needle is not ...

  4. 【LeetCode 8_字符串_实现】String to Integer (atoi)

    , INVALID}; int g_status; long long SubStrToInt(const char* str, bool minus) { ; : ; while (*str != ...

  5. 【LeetCode 38_字符串_算术运算】Count and Say

    string countAndSay(int n) { string res; ) return res; res = "; ) { int len = res.size(); int i, ...

  6. 【LeetCode 67_字符串_算术运算】Add Binary

    string addBinary(string a, string b) { int alen = a.size(); int blen = b.size(); ) return b; ) retur ...

  7. Leetcode中字符串总结

    本文是个人对LeetCode中字符串类型题目的总结,纯属个人感悟,若有不妥的地方,欢迎指出. 一.有关数字 1.数转换 题Interger to roman和Roman to integer这两题是罗 ...

  8. LeetCode OJ:Implement strStr()(实现子字符串查找)

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

  9. [LeetCode] Implement strStr() 实现strStr()函数

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

随机推荐

  1. eclipse上的.properties文件中文编辑显示处理

    最近在对接银联备份金,将相应的SDK导入到eclipse后,打开.properties文件中文注释变成了如下样子,很不方便查阅参照: 平常开发我们希望看到的是如下样子,很直观能明确配置的参数代表的信息 ...

  2. LVM2逻辑卷创建及扩容

    LVM是Logical Volume Manager(逻辑卷管理器)的简写,又译为逻辑卷宗管理器.逻辑扇区管理器.逻辑磁盘管理器.是Linux核心所提供的逻辑卷管理(Logical Volume Ma ...

  3. PreparedStatement和Statement区别详解

    技术原理 该 PreparedStatement接口继承Statement,并与之在两方面有所不同: PreparedStatement 实例包含已编译的 SQL 语句.这就是使语句“准备好”.包含于 ...

  4. ElasticSearch(三) ElasticSearch中文分词插件IK的安装

    正因为Elasticsearch 内置的分词器对中文不友好,会把中文分成单个字来进行全文检索,所以我们需要借助中文分词插件来解决这个问题. 一.安装maven管理工具 Elasticsearch 要使 ...

  5. localAddress

    $(function(){ <% out.println("/** ip:"+request.getLocalAddr()+"("+request.get ...

  6. RabbitMQ单机多实例配置

    由于某些因素的限制,有时候你不得不在一台机器上去搭建一个rabbitmq集群,当然这种集群只适合自己玩玩,验证下结论,这个有点类似zookeeper的单机版.真实生成环境还是要配成多机集群的.有关怎么 ...

  7. MySQL INSERT语句

    insert的语法 INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] ...

  8. Oracle——ORA-01031: 权限不足

    报错:ORA-01031: 权限不足 解决方法:给用户admin授予DBA角色 grant dba to admin;

  9. Docker run 挂载 volume 记录

     docker run -i -t -v /f/a:/f/ centos:7 /bin/bash -v 本地路径 : 挂载到 centos 中  f 文件夹 中 windows10 中  不能使用 f ...

  10. Struts 2 + Hibernate + Spring 整合要点

    Struts 2 和 Spring 的功能有重合,因此有必要说明下,整合中分别使用了两种框架的哪些技术. Struts 2 使用功能点: 1.拦截器.一处是对非登录用户购物进行拦截,一处是对文件上传的 ...