实现 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() 定义相符。

class Solution {
public int strStr(String haystack, String needle) {
if (needle.length() == 0) {
return 0;
}
for (int i=0;i<=haystack.length()-needle.length();i++) {
if (haystack.subSequence(i, needle.length()+i).equals(needle)) {
return i;
}
}
return -1;
}
}

LeetCode28.实现strStr()的更多相关文章

  1. LeetCode28 Implement strStr()

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

  2. [Swift]LeetCode28. 实现strStr() | Implement strStr()

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

  3. leetcode-28.实现strStr()

    leetcode-28.实现strStr() 题意 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字 ...

  4. LeetCode28.实现strStr() JavaScript

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  5. LeetCode28——实现strStr()

    6月中下旬辞职在家,7 月份无聊的度过了一个月.8 月份开始和朋友两个人写项目,一个后台和一个 APP ,APP 需要对接蓝牙打印机.APP 和蓝牙打印机都没有搞过,开始打算使用 MUI 开发 APP ...

  6. leetcode28 strstr kmp bm sunday

    字符串匹配有KMP,BM,SUNDAY算法. 可见(https://leetcode-cn.com/problems/implement-strstr/solution/c5chong-jie-fa- ...

  7. [PHP源码阅读]strpos、strstr和stripos、stristr函数

    我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...

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

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

  9. strstr 函数的实现

    strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...

随机推荐

  1. 关于JAVA项目中的常用的异常处理情况

         Java异常处理的几个原则如下.       1)不要丢弃异常,捕获异常后需要进行相关处理.如果用户觉得不能很好地处理该异常,就让它继续传播,传到别的地方去处理,或者把一个低级的异常转换成应 ...

  2. 扒来的lstdc++.6.0.9有效解决方案

    找到了Xcode10模拟器和真机运行的办法了1.将libstdc++_sim.zip解压,放到目录 /Applications/Xcode.app/Contents/Developer/Platfor ...

  3. 指数级计算复杂度 调用Fibonacci函数次数

    指数级计算复杂度 计算调用次数 #include <stdio.h> long fibonacciCallTimes(long n); int main(void) { long resu ...

  4. Mac 下的 C++ 开发环境

    1. Xcode 创建 C++ 项目 Xcode (版本 4.6.3)默认支持创建 C++ 项目,步骤很简单:打开 Xcode,新建一个项目:在 OS X 中的 Application 中选择 Com ...

  5. SRTP讨论

    1.接下来任务如何分工? 2.接下来要完成哪些工作? 硬件上要完成哪些? 1)环境数据的采集(我) 2)对小车的控制(我) 3)路径规划(喻) 4)小车的避障(雷) 5)环境数据的处理融合(我) 6) ...

  6. jquery基础学习之DOM篇(二)

    在此之前请牢记,jquery 是一个合集对象!!!! 1.节点创建 js创建方法: 创建元素:document.createElement 设置属性:setAttribute 添加文本:innerHT ...

  7. thinkphp模板使用

    1.模板文件 就是个html,可以保存到View的Public文件夹下,比如叫base.html(参考onethink) <block name="a">a</b ...

  8. 卸载postgresql数据库

    卸载postgresql数据库有两种方法,第一种一个个包卸载,第二种全部卸载.做这些之前请先做好备份,以防意外! 1.1.查询出postgres数据库,用命令rpm -qa |grep postgre ...

  9. 用composer安装laravel-bjyblog

    前面讲了两行命令composer的安装,现在我们来操作一下composer安装基于laravel的博客laravel-bjyblog.测试环境是linux,bt面板,php7.2安装扩展fileinf ...

  10. docker+jenkins的构建历史记录(Build History)时间不正确

    1.分别查看宿主机时间和容器时间 宿主机时间 root@fcaad17f146a:/# date Fri Jan :: CST 容器时间 [root@ ~]# docker exec -ti 8798 ...