【LeetCode】Implement strStr()(实现strStr())
这道题是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())的更多相关文章
- 乘风破浪:LeetCode真题_028_Implement strStr()
乘风破浪:LeetCode真题_028_Implement strStr() 一.前言 这次是字符串匹配问题,找到最开始匹配的位置,并返回. 二.Implement strStr() 2.1 ...
- leetcode5 Implement strstr() 实现strstr函数功能
Implement strstr() 实现strstr函数功能 whowhoha@outlook.com Question: Implement strstr(). Returns the index ...
- 每日一道 LeetCode (9):实现 strStr()
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [Leetcode] implement strStr() (C++)
Github leetcode 我的解题仓库 https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...
- LeetCode Implement strStr()(Sunday算法)
LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [LeetCode] Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- [leetcode]28. Implement strStr()实现strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode: Implement strStr() [027]
[题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...
随机推荐
- python3爬虫03(find_all用法等)
#read1.html文件# <html><head><title>The Dormouse's story</title></head># ...
- VMware下Centos6.4安装
VMware(Virtual Machine ware)是一个“虚拟PC”软件公司,提供服务器.桌面虚拟化的解决方案. 小伙伴们网上下载VMware11,一路下一步自己安装吧!!! 打开 VMware ...
- 利用photoshop制作gif图片
首先准备你需要的几张素材图片 1.将素材图片根据发生的顺序放置在不同的图层 2.打开窗口下的时间轴 选择帧动画 3.创建第一张帧动画 选项卡右边这个按钮,点击这个选择新建帧 第一张图片显示其他的隐藏 ...
- hihocoder 1080 线段树(区间更新)
题目链接:http://hihocoder.com/problemset/problem/1080 , 两种操作的线段树(区间更新). 这道题前一段时间一直卡着我,当时也是基础不扎实做不出来,今天又想 ...
- 使用SAP云平台 + JNDI访问Internet Service
以Internet Service http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Walldorf&destin ...
- 站点安全预警,建议大家多重禁止load_file函数!
比如在你的linux机器上运行 select load_file(0x2F6574632F706173737764); 看看结果是什么?这应该不是我们希望看到的. 所以我们禁用这个函数吧. 这个主要通 ...
- clearerr, feof, ferror, fileno - 检查以及重置流状态
总览 (SYNOPSIS) #include <stdio.h> void clearerr(FILE *stream); int feof(FILE *stream); int ferr ...
- USACO09FEB Fair Shuttle
题目传送门 据说\(NOIp\)前发题解可以\(\mathfrak{RP}\)++ 因为要尽可能满足更多奶牛,所以按照这种区间贪心题的套路,先按右端点排序,然后依次遍历,能坐车的就让它们坐车,这样一定 ...
- 深入浅出:了解for循环中保留i值得方法
一.保留i值 通常情况下,因为一些效果我们需要获取到for循环中的i的值,但是往往拿到的都是最后一个i的值.下面介绍几种方法可以获取到i的值 1.自定义属性: arr[i].index = i; 以 ...
- 权限组件(15):rbac的使用文档和在业务中的应用
这里用主机管理系统当做示例. 一.将rbac组件拷贝到项目中. 注意: rbac自己的静态文件.layout.html(被继承的模板).bootstrap.fontsize.公共的css.jquery ...