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. C# 委托Delegate的使用 笔记

    使用delegate总是一头雾水,记录一下笔记,备忘. 主要用于线程间操作UI上的控件,以便使用.或者是大家统一操作入口使用. using System.Windows.Forms; namespac ...

  2. fitnesse - 安装部署

    fitnesse - 安装部署 2017-09-29 1 先决条件Java环境 确保机器上装了java, java -version 2 安装fitnesse http://fitnesse.org/ ...

  3. html2canvas不能识别svg的解决方案

    最新有个功能需要截取网页成图片,于是用到比较流行的html2canvas,本来以为能顺顺利利的搞定,后来发现网页上的流程图连接线不在截图中.于是各种百度.bing,也搜到好多,但是感觉没有一个完整的代 ...

  4. redis笔记总结之redis数据类型及常用命令

    三.常用命令 3.1 字符串类型(string) 字符串类型是Redis中最基本的数据类型,一个字符串类型的键允许存储的数据的最大容量为512MB. 3.1.1 赋值与取值: SET key valu ...

  5. Lintcode212 Space Replacement solution 题解

    [题目描述] Write a method to replace all spaces in a string with%20. The string is given in a characters ...

  6. PAT乙级-1070. 结绳(25)

    给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连.每次串连后,原来两段绳子的长度 ...

  7. 关于win系统下Anaconda与TensorFlow的安装相关事宜以及错误:ImportError: No module named 'tensorflow'的解决

    1.安装TensorFlow之前应该先安装Anaconda,不需要安装python,否则会出问题,我安装的版本是Anaconda3-4.2.0-Windows-x86_64,在这个链接上可以找到--h ...

  8. 在线教育平台搭建 预览和models

    一.前言 1.1.项目介绍 在线演示地址:mxonline.mtianyan.cn 开发环境: python:3.6.4 Django:2.0.2 后台管理:xadmin 系统概括: 系统具有完整的用 ...

  9. 微信小程序调接口常见问题解决方法

    第一次调接口时遇见的bug. 注意:接口的域名不能使用 IP 地址或 localhost,且不能带端口号: 微信小程序如何调接口? wx.request({ url: 'http://miniapp/ ...

  10. shell常见脚本30例

    shell常见脚本30例 author:headsen chen  2017-10-19  10:12:12 本文原素材出自网上,特此申明.有些地方加入我自己的改动 常见的30例shell脚本 1.用 ...