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. 【HDU4307】Matrix

    本篇博客基本全篇转自https://www.cnblogs.com/staginner/archive/2012/08/13/2636826.html,太强啦ORZ 题意 A是个1*n的矩阵,每个元素 ...

  2. shell cut 用法

    cut -f   提取第几列 -d  按指定的分隔符割列 cut -f 1 xxx.txt   提取第1列 cut -f 1,3 xxx.txt   提取第1,3列 cut -d ":&qu ...

  3. Opencv3 Mat对象构造函数与常用方法

    构造函数 Mat() Mat(int rows,int cols,int type) Mat(Size size,int type) Mat(int rows,int cols,int type,co ...

  4. 455. Assign Cookies 满足欲望 分配饼干

    [抄题]: Assume you are an awesome parent and want to give your children some cookies. But, you should ...

  5. Luogu 4755 Beautiful Pair

    分治 + 主席树. 设$solve(l, r)$表示当前处理到$[l, r]$区间的情况,我们可以找到$[l, r]$中最大的一个数的位置$mid$,然后扫一半区间计算一下这个区间的答案. 注意,这时 ...

  6. oracle 序列初始化的plsql块脚本

    declare seq_name dba_sequences.SEQUENCE_NAME%TYPE; cursor mycur is select * from dba_sequences where ...

  7. Shiro——概述

    Apache Shiro 是 Java 的一个安全(权限)框架. Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE 环境,也可以用在 JavaEE 环境. Shiro 可以完成 ...

  8. 第三周Linux编程实例练习

    通过以下程序来练习 head.h # ifndef HEAD_H #define HEAD_H #include <stdio.h> int add(int,int); int sub(i ...

  9. css总结10:父标签没有定义高度,盒子异常移动

    1 问题:在父标签没有定义高度的情况下,嵌套的盒子浮动后,父标签下面的元素发生位置错误. 2 解决方法: 2.1(大厂网页常用方法) 添加额外元素: 即:父标签下添加一个元素(.clearfix),去 ...

  10. linq to sql 实现左(右)连接,那个方法是对的,该怎么处理

    linq to sql 实现左(右)连接,那个方法是对的var query2 = from tb0 in db.table_0  join tb1 in db.table_1 on table_0.关 ...