题目:

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

对于一个给定的 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. jQuery增加删除修改tab导航特效

    HTML:         <div class="container iden_top">                <ul>             ...

  2. 【Qt】命令行编译Qt程序(nmake)【转】

    简述 前两节讲解了如何在Visual Studio和Qt Creator中搭建Qt开发环境,并分享了我们第一个小程序-Hello World. 下面分享如何使用命令行来编译Qt程序.当然,MSVC和M ...

  3. silverlight 生成二维码

    MainPage.xaml <Grid x:Name="LayoutRoot" Background="White"> <Border Bor ...

  4. TCP HelloWord

    client.cpp #include <stdio.h> #include <winsock2.h> #pragma comment (lib,"ws2_32&qu ...

  5. string和stringBuilder的区别

    曾经被问到过这个问题,回答得不是很好,在网上找了一下,园子里有大神很详细地讨论了二者的区别. http://www.cnblogs.com/yunfeng8967/articles/1093832.h ...

  6. Requests库的几种请求 - 通过API操作Github

    本文内容来源:https://www.dataquest.io/mission/117/working-with-apis 本文的数据来源:https://en.wikipedia.org/wiki/ ...

  7. sqlalchemy - day1

    一.Create engine Database url规则: dialect+driver://username:password@host:port/database echo: True表示cm ...

  8. 管道和FIFO

    pipe 子进程从终端读取一个文件名, 通过管道将文件名传递给父进程 父进程收到文件名后, 读取文件内容并通过管道传递给子进程 子进程接收到文件内容并输出到终端 #include <stdio. ...

  9. wrk 网站压力测试

    下载安装 wrk [root@aikaiyuan ~]# git clone https://github.com/wg/wrk.gitInitialized empty Git repository ...

  10. jQuery 单选按钮切换

    html代码片段一: <div class="row"> <div class="col-sm-12"> <label for=& ...