【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 ...
随机推荐
- 【微软大法好】VS Tools for AI全攻略(3):低配置虚拟机也能玩转深度学习,无需NC/NV系列
接着上文,现在我们需要一种穷人的方法来搭建好Azure虚拟机. 思路很简单,因为AI组件的原理其实是传送了script文件和命令上去,那么我们这个虚拟机只要做好了所有的配置,那么我们就可以将它当作深度 ...
- UOJ#210. 【UER #6】寻找罪犯 2-sat
#210. [UER #6]寻找罪犯 链接:http://uoj.ac/problem/210 想法:2-sat模型.每个人拆点,分别表示为犯人.非犯人.每个句供词拆点,分别表示真话.假话.供词与对应 ...
- pta 编程题10 Root of AVL Tree
其它pta数据结构编程题请参见:pta 这道题考察平衡二叉查找树的插入. 为了保证二叉查找树的平衡,当一个结点的左右子树的高度差大于1时就要进行调整. 分为以下四种情况: 插入新节点后,以及旋转之后, ...
- autoreleasing on a thread
So basically, if you are running on OS X 10.9+ or iOS 7+, autoreleasing on a thread without a pool s ...
- 理解MVC 框架
前言:很多前端开发者面临着这样的问题,在项目开发中承担的工作越来越多,后端要做的越来越少,需要的技术棧越来越多,经常有人问你个技术是你完全不会的,对自己的职业生涯越来越怀疑.从前认为HTML+CSS+ ...
- NFS服务器实现文件共享
NFS服务器运行原理 实战配置NFS服务器 配置Samba服务器及实现文件共享 (一)NFS器服务端描述 NFS服务器: Network File System,网络文件系统使FreeBSD支持的一种 ...
- python_47_Python2中字符编码与转码
#python3默认是Unicode,Unicode是万国码,不管中文字符还是英文,所有的每个字符都占2个字节空间,16位 #python2默认是ascii码 #ascii码不能存中文,一个英文只能占 ...
- 博学谷-数据分析pandas
import pandas as pd df=pd.read_csv() df=pd.read_sql()
- react的Redux基础
redux的中文文档:http://www.redux.org.cn/ redux的英文官网:https://redux.js.org/ redux相当于vuex Redux 是 JavaScript ...
- 4.在Cisco Packet Tracerl里路由器密码重置
在路由器的特权模式的密码忘记的情况下,关闭路由器的电源,在接通电源,在路由器载入的时候,按ctrl+c,直接进入monitor模式 输入:confreg 0x2142 reset 重新进入后 enab ...