028 Implement strStr() 实现 strStr()
实现 strStr()。
返回蕴含在 haystack 中的 needle 的第一个字符的索引,如果 needle 不是 haystack 的一部分则返回 -1 。
例 1:
输入: haystack = "hello", needle = "ll"
输出: 2
例 2:
输入: haystack = "aaaaa", needle = "bba"
输出: -1
详见:https://leetcode.com/problems/implement-strstr/description/
Java实现:
方法一:暴力解
class Solution {
public int strStr(String haystack, String needle) {
int hsize=haystack.length();
int nsize=needle.length();
int i=0;
int j=0;
while(i<hsize&&j<nsize){
if(haystack.charAt(i)==needle.charAt(j)){
++i;
++j;
}else{
i=i-j+1;
j=0;
}
}
if(j==nsize){
return i-j;
}else{
return -1;
}
}
}
方法二:KMP算法
class Solution {
public int strStr(String s, String p) {
int i=0;
int j=-1;
int ps=p.length();
int[] next=new int[ps+1];
next[0]=-1;
while(i<ps){
if(j==-1||p.charAt(i)==p.charAt(j)){
++i;
++j;
next[i]=j;
}else{
j=next[j];
}
}
int ss=s.length();
i=0;
j=0;
while(i<ss&&j<ps){
if(j==-1||s.charAt(i)==p.charAt(j)){
++i;
++j;
}else{
j=next[j];
}
}
if(j==ps){
return i-j;
}
return -1;
}
}
028 Implement strStr() 实现 strStr()的更多相关文章
- leetcode5 Implement strstr() 实现strstr函数功能
Implement strstr() 实现strstr函数功能 whowhoha@outlook.com Question: Implement strstr(). Returns the index ...
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【LeetCode】028. Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode 028 Implement strStr()
题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [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() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode Implement strStr() 实现strstr()
如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...
- 【LeetCode】Implement strStr()(实现strStr())
这道题是LeetCode里的第28道题. 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle ...
随机推荐
- BZOJ3680:吊打XXX
我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html 我对爬山的理解:https://www.cnblogs.com/AKMer/p/95552 ...
- Poj 2602 Superlong sums(大数相加)
一.Description The creators of a new programming language D++ have found out that whatever limit for ...
- SqlServer2005的备份和还原(不同服务器)
1 备份数据库NorthSJ 进入服务器,进入SqlServer2005,选择数据库NorthSJ进行备份
- binlog之二:怎么样安全删除mysql下的binlog日志
删除binlog方法 第一种方法: mysql> show binary logs; 查看mysql bin-log日志,除了这个以外的,其它都可以使用删除.mysql> purge bi ...
- 查看,上传crushmap命令
标签(空格分隔): ceph,ceph运维,crushmap 查看crushmap命令 从mon节点获取crushmap: # ceph osd getcrushmap -o crush.map 反编 ...
- PHP7三元运算符 ?? 和 ?: 的区别
1. (expr1) ?? (expr2) 是 PHP7才有的功能,等同于: isset(expr1) ? expr1 : expr2 ; 2.(expr1) ?: (expr2) 是PHP5.3才 ...
- LAMP 1.5 测试PHP解析 问题解决
安装完php从新加载了一个模块 php5_module 重新启动模块 /usr/local/apache2/bin/apachectl restart ifconfig 查看本机ip,在浏览器里面输入 ...
- new/delete 和malloc/free 的区别
new/delete 和malloc/free 的区别 一.基本概念malloc/free:1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumB ...
- 使用Meshlab软件将点云(Point Cloud)转换为模型(Mesh)
使用Meshlab软件将点云(Point Cloud)转换为模型(Mesh) 启动Meshlab软件: 导入.ply点云文件: 接着点击: 弹出一个右侧边栏: 接着,计算每个点的法线: 输入100,点 ...
- Ubuntu下crontab命令的用法
cron是一个Linux下的后台进程,用来定期的执行一些任务.因为我用的是Ubuntu,所以这篇文章中的所有命令也只能保证在Ubuntu下有效,但其他系统应该也差不多. 想要让cron执行你指定的任务 ...