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. c++ 流状态

    这里是对cin中函数的作用的补充.

  2. Win10系统优化/设置脚本

    Win10系统优化/设置脚本 用了很长时间win10了,用的过程中,发现了一些问题,关于系统基本的优化,和个人的使用习惯设置等等,做成了一个脚本,可以一键设置win10的系统设置,结合DWS对Win1 ...

  3. 11-vector的使用

    C++ vector用法(详解!!函数,实现) 原创 2016年09月30日 01:13:40 7862 1,简述一下vector的基本操作,它的size,capacity(),clear,rever ...

  4. RabbitMQ(pika模块)

    RabbitMQ 基础 2 3 4 5 6 7 8 安装配置epel源    $ rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-r ...

  5. python字符串大小写转换

    str = "www.w3cSChool.cn"print(str.upper()) # 把所有字符中的小写字母转换成大写字母print(str.lower()) # 把所有字符中 ...

  6. Java设计模式(1)——简单工厂模式

    简单工厂模式属于类的创建型模式,又叫做静态工厂方法模式.通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类. 一.模式中包含的角色及其职责 1.工厂(Creator)角色 简单 ...

  7. 利用Thread.stop完成方法执行超时中断

    示例代码可以从github上获取 https://github.com/git-simm/simm-framework.git 接上篇博客<FutureTask子线程取消执行的状态判断> ...

  8. MongoDB整理笔记の管理Sharding

    1.列出所有的Shard Server > db.runCommand({ listshards: 1 }) --列出所有的Shard Server { "shards" : ...

  9. 关于.net DateTime 的一些事儿

    最近开发的过程中遇到一种情况,在.net 程序中获取的Datetime格式的时间,在存入SQL server中,毫秒部分丢失. 这个是个很奇怪的状况,因为在Debug的时候,Datetime的变量的确 ...

  10. 【SQL】- 基础知识梳理(七) - 索引

    索引的概念 在关系型数据库中,索引是对数据库表中一列或多列的值进行排序的一种结构. SQL SERVER中有索引的类型:按存储结构区分:“聚集索引(又称聚类索引,簇集索引)”,“分聚集索引(非聚类索引 ...