字符串的匹配,返回匹配开始的位置,直接用暴力方式求解。为了更快的匹配,定义一个指针表示待匹配的字符串的长度,当长度不足时,可

直接停止匹配。

char *strStr(char *haystack, char*needle)
{
char* p1;
char* p2;
char* p1_advance=haystack; //当字符串数量不足时,直接停止匹配
for (p2 = &needle[]; *p2; p2++)
p1_advance++; for (p1 = haystack; *p1_advance; p1_advance++)
{
char *p1_old = p1;
p2 = needle;
while (*p1 && *p2 && *p1 == *p2)
{
p1++;
p2++;
}
if (!*p2)return p1_old; p1 = p1_old + ;
} return nullptr;
}

leetcode 之Implement strStr()(27)的更多相关文章

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

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

  2. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  3. 【leetcode】Implement strStr() (easy)

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

  4. 【leetcode】Implement strStr()

    Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...

  5. Java for LeetCode 028 Implement strStr()

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

  6. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  7. [LeetCode] 28. Implement strStr() 解题思路

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

  8. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  9. Leetcode 28——Implement strStr()

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

  10. leetcode(57)- Implement strStr()

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

随机推荐

  1. 最小角回归 LARS算法包的用法以及模型参数的选择(R语言 )

    Lasso回归模型,是常用线性回归的模型,当模型维度较高时,Lasso算法通过求解稀疏解对模型进行变量选择.Lars算法则提供了一种快速求解该模型的方法.Lars算法的基本原理有许多其他文章可以参考, ...

  2. POJ3648:Wedding——题解(配2-SAT简易讲解)

    http://poj.org/problem?id=3648 (在家,而且因为2-SAT写的不明不白的,所以这篇详细写) 题目大意: 有一对新人结婚,邀请了n-1 对夫妇去参加婚礼.婚礼上所有人要坐在 ...

  3. Android源码4.4.4_r1下载和编译

    系统:ubuntu 16.04.2 TLS 1.源码下载: sudo apt-get install curl curl https://storage.googleapis.com/git-repo ...

  4. Django Session配置

    Django Session的三种存储方式 SESSION_ENGINE='django.contrib.sessions.backends.db' # default 保存到数据库中,依赖 'dja ...

  5. mac命令行配置网络

    mac命令行配置网络今天终于找到了Mac OS X通过命令行修改ip的方式了,记录如下: 修改mac地址,重启后失效sudo ifconfig en0 lladdr d0:67:e5:2e:07:f1 ...

  6. UVA 1649 Binomial coefficients

    https://vjudge.net/problem/UVA-1649 题意: 输入m,求所有的C(n,k)=m m<=1e15 如果枚举n,那么C(n,k)先递增后递减 如果枚举k,那么C(n ...

  7. 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

  8. linux下安装tomcat8

    1.自己电脑下载好jdk的linux版本传到linux上或者直接用wget命令下载 安装文件放上去,用ls命令查看下载后的文件,看到apache-tomcat-8.0.28.tar.gz就是我们下载来 ...

  9. python3中字典的遍历和合并

    #字典的遍历方式 dic={"a":1,"b":2,"c":3} for k in dic: print (k,dic[k]) for k, ...

  10. RecycleView Bug:java.lang.IndexOutOfBoundsException: Inconsistency detected.

    今天使用RecyclerView时,上下两个RecyclerView,在实现下拉刷新时,报错: java.lang.IndexOutOfBoundsException: Inconsistency d ...