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. centos7.2 配置内网ntp服务器进行时间同步

    (一)修改/etc/ntp.conf 配置文件,注意红色部分,其他部分不需要改  ########################################################### ...

  2. 网页提示错误(net::ERR_EMPTY_RESPONSE)

    突然个别网页打不开,报上面的错,本来还以为是网页的问题,结果发现是自己的电脑的问题..因为从别的设备上可以打开相同网页. 1.运行→regedit→进入注册表, 在 HKEY_LOCAL_MACHIN ...

  3. NancyFX 第十二章 通道截拦

    所有的好的Web框架都有一套好的通道截拦的机制,Nancy在我看来是处理最好的.那什么是请求通道那?下面的图可能说的比较清楚些: 正如名称中描述的,一个典型的Web请求在到达最终响应前会穿过一定数量的 ...

  4. JAVA线程sleep和wait方法区别

    一. sleep 是线程类(Thread)的方法,导致此线程暂停执行指定时间,给执行机会给其他线程,但是监控状态依然保持,到时后会自动恢复,调用sleep 不会释放对象锁.由于没有释放对象锁,所以不能 ...

  5. C++实现控制台版2048

    前言 之前做过一个JavaScript版本的2048游戏,最近在学习C++,昨天晚上突然心血来潮,想用C++来实现,因为核心算法已十分理解,所以两个小时撸出来一个C++的简易版本. 简介 二维数组遍历 ...

  6. NOI2001炮兵阵地

    题目传送门 PS:本道题目建议在对状压dp有一定了解的基础下学习,如有不懂可以先去学一下状压dp入门 题目大意:给你n*m个格子,有些格子可以用来部署军队,用P表示,有些则不能,用H表示,如果在一个格 ...

  7. Python账号密码登陆判断(三次机会)

    #!/usr/bin/env python #coding=UTF-8 #先设定初始用户名和登录密码 init_usrname=input("Please enter initial use ...

  8. 大数据Hadoop与Spark学习经验谈

    昨晚听了下Hulu大数据基础架构组负责人–董西成的关于大数据学习方法的直播,挺有收获的,下面截取一些PPT的关键内容,希望对正在学习大数据的人有帮助. 现状是目前存在的问题,比如找百度.查书这种学习方 ...

  9. 设计模式——状态模式(C++实现)

    /////////context.cpp #include "context.h" void STContext::ChangeState(STState* pstState) { ...

  10. Spring服务定制

    问题总述 ​ 我们都知道如果使用Spring来进行bean管理的时候.如果同一个接口的实现类存在两个,直接使用@Autowired注解来实现bean注入,会在启动的时候报异常.我们通常的做法是使用@R ...