这道题是LeetCode里的第28道题。

题目描述:

实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1

示例 1:

输入: haystack = "hello", needle = "ll"
输出: 2

示例 2:

输入: haystack = "aaaaa", needle = "bba"
输出: -1

说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

简而言之就是要我们实现获取子串起始位置的函数,就是在 haystack 中 找 needle。同样是双指针。为了提高效率我们以 needle 字符串的长度为一个长度区间,然后在这个区间里由两边向中间遍历每一个字符。

解题代码:

class Solution {
public int strStr(String haystack, String needle) {
if(haystack.length() < needle.length())return -1;
int space = needle.length();
if(space == 0)return 0;
boolean flag = false;
for(int i=0; i<=haystack.length() - space; i++){
if(haystack.charAt(i) == needle.charAt(0) &&
haystack.charAt(i + space - 1) == needle.charAt(space - 1)){
for(int j=1; j<=(int)space/2; j++){
flag = true;
if(haystack.charAt(i + j) != needle.charAt(j) ||
haystack.charAt(i + space - 1 - j) != needle.charAt(space - 1 - j)){
flag = false;
break;
}
}
if(flag || space == 1)return i;
}
}
if(space == haystack.length()){
for(int i=0; i<(int)space/2; i++){
flag = true;
if(haystack.charAt(i) != needle.charAt(i) ||
haystack.charAt(space - 1 - i) != needle.charAt(space - 1 - i)){
flag = false;
break;
}
}
if(flag || space == 1)return 0;
}
return -1;
}
}

提交结果:

个人总结:

双指针在字符串方面用的比较多,但是对于字符串双指针并不是唯一的解。

【LeetCode】Implement strStr()(实现strStr())的更多相关文章

  1. 乘风破浪:LeetCode真题_028_Implement strStr()

    乘风破浪:LeetCode真题_028_Implement strStr() 一.前言     这次是字符串匹配问题,找到最开始匹配的位置,并返回. 二.Implement strStr() 2.1 ...

  2. leetcode5 Implement strstr() 实现strstr函数功能

    Implement strstr() 实现strstr函数功能 whowhoha@outlook.com Question: Implement strstr(). Returns the index ...

  3. 每日一道 LeetCode (9):实现 strStr()

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

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

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

  5. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

  6. LeetCode Implement strStr()(Sunday算法)

    LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...

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

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

  8. [LeetCode] Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  9. [leetcode]28. Implement strStr()实现strStr()

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

  10. LeetCode: Implement strStr() [027]

    [题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...

随机推荐

  1. SQL重复记录查询-count与group by having结合查询重复记录

    查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select  peopleId  from  p ...

  2. Coursera 算法二 week 3 Baseball Elimination

    这周的作业不需要自己写算法,只需要调用库函数就行,但是有些难以理解,因此用了不少时间. import edu.princeton.cs.algs4.FlowEdge; import edu.princ ...

  3. java之打印机服务通俗做法

    javax.print包是API的主包,其中包含的类和接口能够让你:1)发现打印服务(Print Services)2)指定打印数据的格式 3)从一个打印服务创建打印工作(print jobs) 4) ...

  4. Windows Profile的一些问题

    电脑症状:桌面复制的文件重启后消失:新安装的软件重启后也自动消失.排查:使用autoruns观察,发现安装了麦咖啡和360两套“安全”软件,除此外并无异常,任务管理器內也无异常发现.过程:1.保险起见 ...

  5. IOS tableView的数据刷新

    1.tableView的刷新 1> 数据刷新的总体步骤 * 修改模型数据 * 刷新表格(刷新界面) 2> 刷新表格(刷新界面)的方法 * 全局刷新(每一行都会重新刷新) - (void)r ...

  6. oc语言特性

    It’s a superset of the C programming language and provides object-oriented capabilities and a dynami ...

  7. Being a Good Boy in Spring Festival(博弈)

    Being a Good Boy in Spring Festival Problem Description一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里尝试做做下面的事情吧 陪妈妈逛一次 ...

  8. 【wiki】红帽linux

    Red Hat Enterprise Linux From Wikipedia, the free encyclopedia wiki 上面红帽的版本信息. https://en.wikipedia. ...

  9. javaweb基础(27)_jsp标签库实例

    一.开发标签库 1.1.开发防盗链标签 1.编写标签处理器类:RefererTag.java 1 package me.gacl.web.simpletag; 2 3 import java.io.I ...

  10. 跟我一起从零开始学WCF系列课程

    http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/Series/WCF_Begin.aspx 服务和协定 服务协定使 ...