这道题是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. WORD窗体保护密码清除

    Word 2003破解方法如下:1.用Word打开已设置有密码的“保护文档”(原始DOC文件):2.菜单中选择“文件”→“另存为Web页”,保存为HTML文件然后关闭Word:3.用文本编辑器(如:记 ...

  2. 500 Days Of Summer

    <和莎莫的500天>,一部爱情片. Summer和Tom两人不同的爱情观走在了一起,或许真的就是爱情观不同,或许是Summer爱Tom爱的不够深,最终的结局是那么不尽人意. 有人愿意把秘密 ...

  3. URL跨项目调用方法,获取返回的json值,并解析

    package com.mshc.util; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...

  4. IOS类似9.png

    图形用户界面中的图形有两种实现方式,一种是用代码画出来,比如Quartz 2D技术,狠一点有OpenGL ES,另一种则是使用图片. 代码画的方式比较耗费程序员脑力,CPU或GPU; 图片则耗费磁盘空 ...

  5. IOS Block动画

    ● + (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)dur ...

  6. 【BZOJ1013】[JSOI2008] 球形空间产生器(高斯消元)

    点此看题面 大致题意: 给定一个\(n\)维球体上的\(n+1\)个点,请你求出这个球体的圆心的位置. 列出方程 这一看就是一道解方程题. 我们可以设这个球体的圆心的位置为\((x_1,x_2,..x ...

  7. 2018.6.19 Java核心API与高级编程实践复习总结

    Java 核心编程API与高级编程实践 第一章 异常 1.1 异常概述 在程序运行中,经常会出现一些意外情况,这些意外会导致程序出错或者崩溃而影响程序的正常执行,在java语言中,将这些程序意外称为异 ...

  8. centos 7 虚拟机启用网卡

    1.vi /etc/sysconfig/network-scripts/ifcfg-enp0s3 2.编辑默认网卡配置文件,将ONBOOT由no改为yes,编辑完成后,按ESC回至命令模板,输入&qu ...

  9. spring-boot自定义启动端口

    有时候我们可能需要启动不止一个SpringBoot,而SpringBoot默认的端口号是8080,所以这时候我们就需要修改SpringBoot的默认端口了.修改SpringBoot的默认端口有两种方式 ...

  10. numpy各函数简介之生成数组函数

    1.empty(shape[, dtype, order]) 依据给定形状和类型(shape[, dtype, order])返回一个新的空数组. 参数: shape : 整数或者整型元组 定义返回数 ...