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. Yii 2 load() 和 save()

    我这里用的数据库是mongo 数据库 为栗子: public function load($data, $formName = null) { $scope = $formName === null ...

  2. Java 基于web service 暴露接口 供外部调用

    package cn.zr.out.outinterface; import java.text.SimpleDateFormat; import java.util.Date; import jav ...

  3. 17-取石子-hdu1846(巴什博奕)

    http://acm.hdu.edu.cn/showproblem.php?pid=1846 Brave Game Time Limit: 1000/1000 MS (Java/Others)     ...

  4. linux下方便的录屏命令

    linux下方便的录屏命令   ffmpeg -f x11grab -s 1024*768 -r 20 -i :0.0 -sameq ~/recode.mpg -r后是刷新屏率,   推出直接Ctrl ...

  5. jQuery--后台主机列表编辑

    先看效果: 要求: 全选,反选和取消 编辑模式下的全选,反选和取消 编辑模式下单选进入编辑状态,取消退出编辑状态 表格元素有可编辑,不可编辑,下拉选择 按住ctrl选择下拉框,下面的同列选项都随之改变 ...

  6. UOJ#46. 【清华集训2014】玄学

    传送门 分析 清华集训真的不是人做的啊嘤嘤嘤 我们可以考虑按操作时间把每个操作存进线段树里 如果现在点x正好使一个整块区间的右端点则更新代表这个区间的点 我们不难发现一个区间会因为不同的操作被分成若干 ...

  7. 怎样去阅读一份php源代码

    一份好的源代码例如 dz的论坛,wind论坛,帝国cms,dedecms等,都具有自己的一套设计思路和设计模式,所以在看某个产品之前就要做好心理准备,可以把自己的经验和这些产品做对比,但千万别一直用自 ...

  8. Python基础 之 变量、用户交互、if条件语句、while循环语句、编码、逻辑运算

    一.Python介绍 Python 崇尚优美.清晰.简单 Python是一门动态解释型的强制性定义的语言. 二.编译型和解释型的区别 编译型:一次性将所有与程序编译成二进制文件. 缺点:开发效率低,不 ...

  9. Oracle数据库之日期函数

    今天给大家介绍一下oracle数据中的日期函数的用法.废话不多说,我们看一下oracle给我们提供了那些函数? 1.sysdate 用途:获取当前系统时间. 2.to_date('字符类型','日期类 ...

  10. svn ank问题

    subvesion detected a working copy that need upgrade in 可以先在文件夹下cleapup,然后打开解决方案,在解决方案的右键选择upgrade wo ...