Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

实现strStr()。

返回haystack中第一次出现针的索引,如果针不是haystack的一部分,则返回-1。

 int len=0;
if("".equals(needle)) //匹配字符串为空直接返回0
return 0;
if("".equals(haystack)||haystack.length()<needle.length()) //待匹配字符串为空或匹配字符串长度过长
return -1;
for (int i = 0; i < haystack.length() - needle.length()+1; i++) {//利用遍历思想,每次取出匹配字符串长度的字符串来进行匹配比较
String str = haystack.substring(i, needle.length()+i);
if (str.equals(needle)){
len=i;
break;
}
else
len=-1;
}
return len;

LeetCode记录之28——Implement strStr()的更多相关文章

  1. leetCode练题——28. Implement strStr()

    1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...

  2. 【leetcode❤python】 28. Implement strStr()

    #-*- coding: UTF-8 -*- #题意:大海捞刀,在长字符串中找出短字符串#AC源码:滑动窗口双指针的方法class Solution(object):    def strStr(se ...

  3. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  4. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  5. 28. Implement strStr()【easy】

    28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...

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

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

  7. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

  8. C# 写 LeetCode easy #28 Implement strStr()

    28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...

  9. 【LeetCode】28. Implement strStr() 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...

随机推荐

  1. 【NOIP2008】传纸条

    [描述] Description 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就 ...

  2. LoadRunner简明教程

    LoadRunner是什么 LoadRunner是一个性能测试工具,它最初是Mercury公司的产品,后背HP收购. LoadRunner常用来做什么 l 验证某系统在某环境下是否满足性能需求. l  ...

  3. 1-element.src.match("bulbon")

    element.src.match("bulbon")这里的math里面”bulbon“是什么意思? 原代码中 if (element.src.match("bulbon ...

  4. Palindromeness CodeChef - PALPROB

    传送门 分析 有中文题面所以就不写题目大意了 我们先建出回文树 然后根据fail信息建出一棵树 从根向下dfs,中间记录每一个len出现在哪个节点即可 代码 #include<iostream& ...

  5. jquery中children()

  6. 在VS2008和VS2010中禁用Visual Assist X

    此方法对于VS2008和VS2010 都适用. 在VS2008或VS2010菜单栏中选择“VassistX”选项卡,找到“Enable/Disable Visual Assist X”选项, 点击即可 ...

  7. portableDFS-可便携的分布式文件系统

    PPT下载(因附件大小有限制,删除了PPT中的隐藏页,如需完整版本,请转到it168文库下载):portableDFS-可便携的分布式文件系统.ppt 完整版本请上这里下载:http://wenku. ...

  8. .netcore2.0 Startup 全局配置文件小技巧

  9. Invoke()的使用

    (最近在看协程) Invoke()方法是一种委托机制 Invoke ( "SendMsg", 3 ), 意思是3秒之后调用 SendMsg() 方法 使用时应该注意以下几点: 1. ...

  10. RealSense R400系列深度相机的图像获取保存和格式转换

    关于RealSense的基础使用的博文用的库有点混杂,挺多博文都是早期maneger的那个库,对那个不是很了解,主要记录一下使用最新的函数库的基础使用. 相机型号:RealSense R435 使用函 ...