leetcode 之Implement strStr()(27)

字符串的匹配,返回匹配开始的位置,直接用暴力方式求解。为了更快的匹配,定义一个指针表示待匹配的字符串的长度,当长度不足时,可
直接停止匹配。
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)的更多相关文章
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- 【leetcode】Implement strStr() (easy)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【leetcode】Implement strStr()
Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- Leetcode 28——Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- leetcode(57)- Implement strStr()
题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...
随机推荐
- MySQL不支持远程连接的解决办法
如果mysql不支持远程连接,会出现提示:错误代码是1130,ERROR 1130: Host * is not allowed to connect to this MySQL server ,解决 ...
- React获取组件实例
1. 直接new Component() 组件本身也是class,可以new,这样的组件实例意义不大 componentInstance = new Component(); 2. ReactDOM. ...
- [zhuan]VMware中bridge方式网络不能上网的解决办法
http://jingpin.jikexueyuan.com/article/31601.html 安装好VMware 7后,打开原来的虚拟机文件,发现不能上网,原来的Ethernet是设置的Brid ...
- 细谈select函数(C语言)
Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如connect.accept.recv或recvfrom这样的阻塞程序( ...
- STM32 - 软件设置单片机重启
__set_FAULTMASK();//关闭总中断 NVIC_SystemReset();//请求单片机重启 执行NVIC_SystemReset()函数不允许被打断,所以关总中断
- Maatkit--Mysql的高级管理工具
Maatkit是不错的mysql管理工具,已经成为Percona的一部分.包含以下主要工具: 1.mk-table-checksum 检查主从表是否一致的有效工具 2.mk-table-sync 有效 ...
- java中各种循环
简单介绍一下java中的一些循环 package test; import org.apache.log4j.Logger; import org.junit.Test; public class F ...
- RDLC - 后台代码直接导出Excel/PDF/Word格式
最近做报表功能,用到了.net的报表组件rdlc. 其中有个功能就是后台代码直接输出Excel/PDF/Word格式的文件,网上看了些资源,做个总结: 参考地址 我直接贴出代码: //自动导出exce ...
- 【20161109】noip模拟赛
1.Game [题目描述] 明明和亮亮在玩一个游戏.桌面上一行有n个格子,一些格子中放着棋子.明明和亮亮轮流选择如下方式中的一种移动棋子(图示中o表示棋子,*表示空着的格子): 1) 当一枚棋子的右边 ...
- 【Codeforces370E】Summer Reading [构造]
Summer Reading Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample Input 7 0 1 ...