这道题是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. 查看mysql历史命令

    默认情况下操作mysql会在家目录下创建一个隐藏的mysql历史命令文件.mysql_history 在管理授权mysql账户时也会记录这些明文密码到这个文件,非常的不安全 [root@localho ...

  2. Java中的字符串问题

    本文章分为三个部分: 1.创建字符串对象的两种方式以及它们的存储方式 2.String a = new String("a")创建了几个对象的问题 3.字符串小例子 ------- ...

  3. wamp端口冲突

    因为端口冲突,Apache服务不能运行. 解决方法: 点击wamp图标 => Apache => use a port other than 80 => 输入新的端口,即可. 然后 ...

  4. 在编辑Spring的配置文件时的自动提示

    打 开MyEclipse—>Windows--->referenecs——>General,选择下面的Keys,这就是快捷键的设 置,可将Content Assist的快捷键改为 A ...

  5. IOS tabelView退出键盘

    /** *当开始拖拽表格的时候就会调用 * */ -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { //退出键盘 [sel ...

  6. 【51nod1743】雪之国度(最小生成树+倍增)

    点此看题面 大致题意: 给你一张无向连通图,其中每条边的边权为这条边连接的两点的权值之差.每次询问两点之间是否存在两条不重复的路径,若存在则输出这两条路径上最大值的最小值. 大致思路 这题显然就是要让 ...

  7. 解决ssh登录慢,等待时间长的问题

    有时候在ssh远程登录到其他主机上时发现登录时间太长,经过亲自测试,发现主要有两个问题会导致ssh登录慢: 1.使用了dns反查,这样的话当ssh某个IP时,系统会试图通过DNS反查相对应的域名,如果 ...

  8. 使用ASP.NET Web API和Web API Client Gen使Angular 2应用程序的开发更加高效

    本文介绍“ 为ASP.NET Web API生成TypeScript客户端API ”,重点介绍Angular 2+代码示例和各自的SDLC.如果您正在开发.NET Core Web API后端,则可能 ...

  9. 微信小程序页面跳转绑定点击事件

    https://www.cnblogs.com/mrszhou/p/7931747.html

  10. Duizi and Shunzi HDU - 6188 (贪心)2017 广西ACM/ICPC

    Duizi and Shunzi Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...