lintcode:strStr 字符串查找
题目:
字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数。你的任务是实现这个函数。
对于一个给定的 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 字符串查找的更多相关文章
- 字符串函数(strcpy字符串拷,strcmp字符串比较,strstr字符串查找,strDelChar字符串删除字符,strrev字符串反序,memmove拷贝内存块,strlen字符串长度)
1.strcpy字符串拷贝拷贝pStrSource到pStrDest,并返回pStrDest地址(源和目标位置重叠情况除外) char *strcpy(char *pStrDest, const ch ...
- php中常用的字符串查找函数strstr()、strpos()实例解释
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...
- Sunday算法(字符串查找、匹配)
字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).两个算法在最坏情况下均具有线性的查找时间.但是在实用上,KMP算法并不比最简单的 ...
- TCHAR字符串查找&反向查找字符串
C++支持两种字符串,即常规的ANSI编码("字符串")和Unicode编码(L"字符串"),相应的就有两套字符串处理函数,比如:strlen和wcslen,分 ...
- C/C++字符串查找函数
C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...
- C/C++字符串查找函数 <转>
C/C++ string库(string.h)提供了几个字符串查找函数,如下: memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里 ...
- php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos
php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...
- 【实测】Python 和 C++ 下字符串查找的速度对比
完整格式链接:https://blog.imakiseki.cf/2022/03/07/techdev/python-cpp-string-find-perf-test/ 背景 最近在备战一场算法竞赛 ...
- Rabin-Karp指纹字符串查找算法
首先计算模式字符串的散列函数, 如果找到一个和模式字符串散列值相同的子字符串, 那么继续验证两者是否匹配. 这个过程等价于将模式保存在一个散列表中, 然后在文本中的所有子字符串查找. 但不需要为散列表 ...
随机推荐
- LiteHttp:一款‘智能’的HTTP框架类库
LiteHttp:一款‘智能’的HTTP框架类库(转自github) 简介 LiteHttp是一款简单.智能.灵活的HTTP框架库,它在请求和响应层面做到了全自动构建和解析,主要用于Android快速 ...
- scp实现mac与linux服务器之间文件传输
1.mac上传文件到linux服务器 scp 文件名 用户名@服务器ip:目标路径如:scp /Users/test/testFile test@xxx.xxx.xxx.xxx:/test/ 2.ma ...
- Android API Level与sdk版本对照表
API等级1: Android 1.0 API等级2: Android 1.1 Petit Four 花式小蛋糕 API等级3: Android 1.5 Cupcake 纸杯蛋糕 API等级4: An ...
- MvvmCross for WPF File Plugin
本文以MvvmCross为框架基础 最近用了File Plugin插件,一开始也是没用明白,写一下记录下来,也方便需要的人吧 首先这个File Plugin需要先在UI项目里创建一个Bootstrap ...
- ArcGIS操作问题
1.利用分析工具——叠加分析——“空间连接”工具,将完全包含(COMPLETELY_CONTAINS)某点的面的属性值赋为该点的属性值. 其中定义用于匹配行的条件.匹配选项包括: 相交—如果连接要素与 ...
- Nginx模块开发1_明白自定义模块的编译流程
自定义模块的编译流程 --add-module参数 configure使用--add-module参数指定添加模块目录. config脚本 由--add-module指定的目录保存为$ngx-addo ...
- 完整DataTable与IList互换(转)
public class CollectionHelper { private CollectionHelper() { } public static DataTable ConvertTo< ...
- 野指针及c++指针使用注意点
避免野指针的产生 “野指针”的成因主要有: 1)指针变量没有被初始化.任何指针变量刚被创建时不会自动成为NULL指针,它的缺省值是随机的,它会乱指一气.所以,指针变量在创建的同时应当被初始化,要么将指 ...
- java collections
http://www.docjar.com/docs/api/java/util/technotes/guides/collections/changes4.html http://www.docja ...
- centos wordpress
How To Install Linux, nginx, MySQL, PHP (LEMP) stack on CentOS 6 https://www.digitalocean.com/commun ...