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. FCN小小实战

    先说一下前期准备工作:自己的运行环境是Ubuntu16.04+caffe+CPU(这台电脑没有GPU)+python 关于python的搭建就不说了,网上随便一搜,很多参考资源.说一下我配置好caff ...

  2. js监听input输入框值的实时变化实例

    情景:监听input输入框值的实时变化实例 解决方法:1.在元素上同时绑定oninput和onporpertychanger事件 实例:<script type="text/JavaS ...

  3. Session和Cookie总结

    一.Session和Cookie 1.Cookie 1.cookie创建于服务器,保存于浏览器,保存了特定网站操作记录和资料凭证的信息. 2.未设置cookie期限的时候,默认是关闭浏览器后cooki ...

  4. python 数据结构简介

    栈(stack) 定义: 数据集合,只能在一端(首尾)进行删除和插入的列表. 特点: 后进先出(LIFO) 典型作用: 括号匹配:左括号进栈,右括号跟左括号对应则出栈,例如:(({{[]}}))匹配 ...

  5. JVM垃圾收集器&对象的引用回收

    1.介绍垃圾收集器 垃圾收集器(Garbage Collection,GC)就是用于回收方法区和堆区,其他程序计数器.虚拟机栈.本地方法栈这3个区域都是随线程而生,随线程而灭,栈中的栈帧会随着方法的进 ...

  6. 关于OPC UA Helper 命名空间中的OpcUaClient 类中的订阅函数的更改

    原函数是opcUaClient.MonitorValue("ns=4;s=MAIN.d", new Action<double, Action>(MonitorTest ...

  7. call是什么?一次说个明白

    首先简单粗暴的从例子中看概念 var a = {}; function foo() { this.name = "hello"; this.age = 100 } foo.call ...

  8. Selenium webdriver实现截图功能

    可参考http://www.cnblogs.com/tobecrazy/p/3599568.html Webdriver截图时,需要引入: import java.io.File; import ja ...

  9. jQuery中有关mouse的事件--mousedown/up/enter/leave/over/out----2017-05-10

    mousedown:鼠标按下才发生 mouseup:鼠标按下松开时才发生 mouseenter和mouseleave效果和mouseover mouseout效果差不多:但存在区别,区别见代码解析: ...

  10. Cesium 一个导致浏览器内存一直增长的方法

    为了实时更改模型的位置,给模型附上ID,后面判断如果传来的数据中没有已经创建的模型,删掉该模型时用到方法:viewer.entities.removeById(modelId);和viewer.ent ...