实现 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) {
return haystack.indexOf(needle);
}
}

 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.substring(i,i+needle.length()).equals(needle)) return i;
}
return -1;
}
}

2019-04-22 21:17:59

python

方法1:

暴力模式匹配  O(N*M)

 class Solution:
def strStr(self, haystack: str, needle: str) -> int:
i,j = 0,0
while i < len(haystack) and j < len(needle):
if haystack[i]==needle[j]:
i+=1
j+=1
else:
i = i - j + 1
j = 0
if j >=len(needle):
return i - len(needle)
else:
return -1

KMP:shaodeng

LeetCode--028--实现strStr() (java)的更多相关文章

  1. Java for LeetCode 028 Implement strStr()

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

  2. LeetCode 028 Implement strStr()

    题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...

  3. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  4. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  6. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. 前端与算法 leetcode 28.实现 strStr()

    # 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...

  8. Java实现 LeetCode 28 实现strStr()

    28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...

  9. [LeetCode]28.实现strStr()(Java)

    原题地址: implement-strstr 题目描述: 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字 ...

  10. Java [leetcode 28]Implement strStr()

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

随机推荐

  1. nginx如何调用php

    nginx如何调用php 采用nginx+php作为webserver的架构模式,在现如今运用相当广泛.然而第一步需要实现的是如何让nginx正确的调用php.由于nginx调用php并不是如同调用一 ...

  2. 深入理解javascript原型和闭包(转)

    深入理解javascript原型和闭包(完结)   说明: 该教程绕开了javascript的一些基本的语法知识,直接讲解javascript中最难理解的两个部分,也是和其他主流面向对象语言区别最大的 ...

  3. datetimepicker 使用

    首先引入 <%--日历样式--%> <link rel="stylesheet" type="text/css" href="< ...

  4. HashMap 源码分析

    static final int DEFAULT_INITIAL_CAPACITY = 16;  默认容量 static final int MAXIMUM_CAPACITY = 1073741824 ...

  5. RocketMQ基本概念

    RocketMQ基本概念 一.生产者 生产者发送业务系统产生的消息给broker.RocketMQ提供了多种发送方式:同步的.异步的.单向的. 1.1 生产者组 具有相同角色的生产者被分到一组.假如原 ...

  6. 2017-2018-2 20155228 《网络对抗技术》 实验五:MSF基础应用

    2017-2018-2 20155228 <网络对抗技术> 实验五:MSF基础应用 1. 实践内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需 ...

  7. SQL Server 复制表结构以及数据,去除表中重复字段

    --复制另一个数据库中的某张表的结构及数据--select * from Test.dbo.TestTable(查询表中所有数据) --into [表名] 插入当前数据库新表,如果没有该表就创建 se ...

  8. 问题 1672: 迷宫问题 (BFS)

    题目链接:https://www.dotcpp.com/oj/problem1672.html 问题 1672: 迷宫问题 时间限制: 1Sec 内存限制: 32MB 提交: 663 解决: 158 ...

  9. node.js浅见

    看过很多朋友node.js代码敲得很好,但是对于概念还是很生疏.个人认为,代码是树叶,树干搭起来才是王道. 1.简述node.js的适用场景: IIO密集而非计算密集的情景:高并发微数据(比如账号系统 ...

  10. 利用mybatis-generator自动生成代码,发生:Plugin execution not covered by lifecycle configuration后解决方案

    1,报错信息 Plugin execution not covered by lifecycle configuration: org.mybatis.generator:mybatis-genera ...