String Two Pointers

Description:

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

字符串匹配问题,效率最高的是用KMP算法,这里用了双重for循环,但是循环条件控制的不好。

我认为这题想AC的关键是测试用例的编写,一定要想全,至少有以下几个:

  1. 分别是null或"",这两者是有区别的,""是表示有字符串的,只是为空,length() 为0,而null表示没有字符串,不存在length()。共有四种组合。
  2. 完全匹配
  3. 不完全匹配:haystack更长;needle更长

my Solution:

public class Solution {
public int strStr(String haystack, String needle) {
if (haystack != null && needle != null) {
if (needle.isEmpty()) {
return 0;
} else {
int j = 0;
int index = 0;
for (int i = 0; i < haystack.length(); i++) {
index = i;
for (j = 0; j < needle.length(); j++) {
if (haystack.charAt(index) != needle.charAt(j)) {
break;
}
index++;
if (index >= haystack.length()) {
j++;
break;
}
}
if (j == needle.length()) {
return i;
}
}
}
}
return -1;
}
}

改进版:

public class Solution {
public int strStr(String haystack, String needle) {
if (haystack != null && needle != null) {
if (needle.isEmpty()) {
return 0;
} else {
for (int i = 0; i < haystack.length() - needle.length() + 1; i++) {
int j = 0;
for (; j < needle.length(); j++) {
if (haystack.charAt(i+j) != needle.charAt(j)) {
break;
}
}
if (j == needle.length()) {
return i;
}
}
}
}
return -1;
}
}

在第一层循环中改变了终止条件,使得速度上快很多,另外少了index变量,空间复杂度也降了。在核心判断条件:haystack.charAt(i+j) != needle.charAt(j)中,使用了双指针的思想。

LeetCode & Q28-Implement strStr-Easy的更多相关文章

  1. 【leetcode】Implement strStr() (easy)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. [LeetCode] 28. Implement strStr() 实现strStr()函数

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  4. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  5. 【leetcode】Implement strStr()

    Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...

  6. Java for LeetCode 028 Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  8. [LeetCode] 28. Implement strStr() 解题思路

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  10. Leetcode 28——Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

随机推荐

  1. POI导出Excel的几种情况

    第一种:常见导出[已知表头(长度一定),已知表数据(具体一个对象的集合,并已知对象各个属性的类型)]第二种:不常见导出[已知表头(长度不定),已知表数据(没有具体对象,装在String类型的集合中)] ...

  2. 百度在线编辑器 - PHP获取提交的数据

    原文:http://www.upwqy.com/details/14.html 1 我们知道在在百度在线编辑器的demo中. 我们只要在body 里面 加载 script 标签 id="ed ...

  3. Rotation Proposals

    Rotation Proposals 论文Arbitrary-Oriented Scene Text Detection via Rotation Proposals 这篇论文提出了一个基于Faste ...

  4. iframe结构的项目,登录页面嵌套

    参考:http://www.cnblogs.com/qixin622/p/6548076.html 在网页编程时,我们经常需要处理,当session过期时,我们要跳到登陆页面让用户登陆,由于我们可能用 ...

  5. git远程提交失败

    同步仓库并解决403报错 这时候对本地仓库和github进行同步 # git push -u origin master error: The requested URL returned error ...

  6. 第一周CoreIDRAW课总结

      一. 问:这节课学到了什么知识? 答:这节课我学到了很多知识,作为初学者的我,老师给我讲了CorelDEAW的启动,安装,基础知识和应用与用途. 基础知识 位图:是由无数个像素点构成的图像,又叫点 ...

  7. C语言第二次博客作业---分支结构

    一,PTA实验作业 题目1.计算分段函数 本题目要求计算下列分段函数f(x)的值: 1.实验代码 double x,result; scanf("%lf",&x); if( ...

  8. Mycat 分片规则详解--固定 hash 分片

    实现方式:该算法类似于十进制的求模运算,但是为二进制的操作,例如,取 id 的二进制低 10 位 与 1111111111 进行 & 运算 优点:这种策略比较灵活,可以均匀分配也可以非均匀分配 ...

  9. JSON Web Token - 在Web应用间安全地传递信息

    转载自:http://blog.leapoahead.com/2015/09/06/understanding-jwt/ JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使 ...

  10. mysql在线修复主从同步

    要实现MySQL的主从复制,首先必须打开Master端的binlog记录功能,否则就无法实现.因为整个复制过程实际上就是Slave从aster端获取binlog日志,然后再在Slave上以相同顺序执行 ...