题目:

字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数。你的任务是实现这个函数。

对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。

如果不存在,则返回 -1

样例

如果 source = "source" 和 target = "target",返回 -1

如果 source = "abcdabcdefg" 和 target = "bcd",返回 1

挑战

O(n2)的算法是可以接受的。如果你能用O(n)的算法做出来那更加好。(提示:KMP)

说明

在面试中我是否需要实现KMP算法?

  • 不需要,当这种问题出现在面试中时,面试官很可能只是想要测试一下你的基础应用能力。当然你需要先跟面试官确认清楚要怎么实现这个题。

解题:

暴力破解,时间复杂度O(mn)

Java程序:

class Solution {
/**
* Returns a index to the first occurrence of target in source,
* or -1 if target is not part of source.
* @param source string to be scanned.
* @param target string containing the sequence of characters to match.
*/
public int strStr(String source, String target) {
//write your code here if(source==null || target==null)
return -1;
int sourceLen = source.length();
int targetLen = target.length();
if(targetLen==0)
return 0;
if(targetLen>sourceLen)
return -1; for(int i=0;i<sourceLen;i++){
if(sourceLen -i <targetLen)
return -1;
int k = i;
for(int j = 0;j<targetLen;j++){
if(source.charAt(k) == target.charAt(j)){
if(j==targetLen-1)
return i;
k++;
}else
break;
}
}
return -1;
}
}

总耗时: 1332 ms

Python程序:

class Solution:
def strStr(self, source, target):
# write your code here
if source==None or target ==None:
return -1
if len(target)==0:
return 0
sourcelen = len(source)
targetlen = len(target)
for i in range(sourcelen):
k = i
for j in range(targetlen):
if source[k]==target[j]:
if j==targetlen-1:
return i
k+=1
else:
break return -1

总耗时: 299 ms

对于应用KMP算法的,待更新

lintcode:strStr 字符串查找的更多相关文章

  1. 字符串函数(strcpy字符串拷,strcmp字符串比较,strstr字符串查找,strDelChar字符串删除字符,strrev字符串反序,memmove拷贝内存块,strlen字符串长度)

    1.strcpy字符串拷贝拷贝pStrSource到pStrDest,并返回pStrDest地址(源和目标位置重叠情况除外) char *strcpy(char *pStrDest, const ch ...

  2. php中常用的字符串查找函数strstr()、strpos()实例解释

    string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...

  3. Sunday算法(字符串查找、匹配)

    字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).两个算法在最坏情况下均具有线性的查找时间.但是在实用上,KMP算法并不比最简单的 ...

  4. TCHAR字符串查找&反向查找字符串

    C++支持两种字符串,即常规的ANSI编码("字符串")和Unicode编码(L"字符串"),相应的就有两套字符串处理函数,比如:strlen和wcslen,分 ...

  5. C/C++字符串查找函数

    C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...

  6. C/C++字符串查找函数 <转>

    C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...

  7. php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos

    php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...

  8. 【实测】Python 和 C++ 下字符串查找的速度对比

    完整格式链接:https://blog.imakiseki.cf/2022/03/07/techdev/python-cpp-string-find-perf-test/ 背景 最近在备战一场算法竞赛 ...

  9. Rabin-Karp指纹字符串查找算法

    首先计算模式字符串的散列函数, 如果找到一个和模式字符串散列值相同的子字符串, 那么继续验证两者是否匹配. 这个过程等价于将模式保存在一个散列表中, 然后在文本中的所有子字符串查找. 但不需要为散列表 ...

随机推荐

  1. css解决IE6、Chrome、ff 浏览器position:fixed;和闪动问题

    首先说下开发模式,刚刚开始接触,基本沿用web端开发方式,目前开发模式上没有找到的适合的,现在基本这样:1,电脑nginx配置服务器3,电脑和手机连同一个局域网,2,android root 和 ip ...

  2. win8系统下保存出现闪退的解决方案

    不知道有没有人和我一样用的是win8系统,同时还遇到保存QQ截图保存闪退.保存sublime文件闪推.系统自带截图保存闪退.....更可恶的是我用chrome中百度搜索“win8保存东西闪退”结果浏览 ...

  3. extjs的combobox的用法

    可以用javascript的数组作为数据源,也可以用json作为数据源: 1.用javascript数组 var CountryCode = [ ['93','Afghanistan(93)'], [ ...

  4. @RenderSection与@RenderBody

    _LayoutMain: <html> <head> @RenderSection("head") </head> <body> @ ...

  5. haproxy 常用acl规则与会话保持

    一.常用的acl规则 haproxy的ACL用于实现基于请求报文的首部.响应报文的内容或其它的环境状态信息来做出转发决策,这大大增强了其配置弹性.其配置法则通常分为两 步,首先去定义ACL,即定义一个 ...

  6. java 格式化日期(DateFormat)

    import java.text.DateFormat; import java.util.Date; /** * 格式化时间类 DateFormat.FULL = 0 * DateFormat.DE ...

  7. HDU3887 DFS序+ 线段树

    查询树上某个节点的子节点的标号小于其标号的数目. 一个trick是建立线段树之后,从标号小的向标号大的来做更新. 1: #include <cstdio> 2: #include < ...

  8. Large-Scale Deployment of SharePoint Team Services

    http://technet.microsoft.com/en-us/library/cc723713.aspx

  9. 怎样开启SQL数据库服务

    使用数据库时开启服务是需要的,我给大家具体介绍几种方式开启SQL Sever 服务.这几种我都用图文的形式用三个开启方式给你展示,对于不会开启服务的朋友可以学习下,这些前提是你的电脑安装了SQL数据库 ...

  10. C# - 设置DLL的属性Embed Interop Type 设为False

    错误: Error msg: A reference was created to embedded interop assembly. because of an indirect referenc ...