LeetCode--028--实现strStr() (java)

时间:2022-10-09 00:18:43

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

LeetCode--028--实现strStr() (java)

 class Solution {
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
}

LeetCode--028--实现strStr() (java)

 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&lpar;&rpar;

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

  2. LeetCode 028 Implement strStr&lpar;&rpar;

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

  3. LeetCode第&lbrack;18&rsqb;题&lpar;Java&rpar;: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第&lbrack;1&rsqb;题&lpar;Java&rpar;:Two Sum 标签:Array

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

  5. LeetCode第&lbrack;46&rsqb;题&lpar;Java&rpar;:Permutations&lpar;求所有全排列&rpar; 含扩展——第&lbrack;47&rsqb;题Permutations 2

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

  6. LeetCode第&lbrack;1&rsqb;题&lpar;Java&rpar;: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&period;实现 strStr&lpar;&rpar;

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

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

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

  9. Java &lbrack;leetcode 28&rsqb;Implement strStr&lpar;&rpar;

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

  10. 【LeetCode】028&period; Implement strStr&lpar;&rpar;

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

随机推荐

  1. Java 集合类 TreeSet、TreeMap

    TreeMap和TreeSet的异同: 相同点: TreeMap和TreeSet都是有序的集合,也就是说他们存储的值都是拍好序的. TreeMap和TreeSet都是非同步集合,因此他们不能在多线程之 ...

  2. &lbrack;Leetcode&rsqb; Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  3. jQuery基础DOM和CSS操作

    $('#box').html();//获取 html 内容$('#box').text();//获取文本内容,会自动清理 html 标签$('#box').html('<em>www.li ...

  4. NServiceBus教程-消息传递与处理

    nservicebus"的容错默认"设计的一部分,基础设施管理事务自动所以你不需要记住所有的线程和状态管理要素配置. 客户端和服务器 理想情况下,服务器代码处理消息事务,但它往往不 ...

  5. Enter键提交表单

    input type="submit"在360浏览器上不能提交   用了这个 <input type="button" class="btn b ...

  6. ecos的app生命周期

    5种变迁,安装.更新.启动.暂停.卸载,每个app都可以自行维护每种变迁 方法简单,只需要在app/$app_name目录下定义task.php文件 <?php class desktop_ta ...

  7. Backbone视图渲染React组件

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  8. python 之 列表list &amp&semi;&amp&semi; 元组tuple

    目录: 列表 列表基本操作 列表的操作符 列表的函数和方法 元组 介绍: 列表是一种可变的有序集合,可以进行访问.添加和删除操作. 元组是一种不可变的有序集合,可以访问. 1.列表的基本操作 创建列表 ...

  9. Python3学习笔记06-字符串

    可以使用引号('或")来创建字符串. var1 = 'Hello World!' var2 = "Runoob" 在最新的Python 3版本中,字符串是以Unicode ...

  10. JavaScript:document&period;execCommand&lpar;&rpar;的用法

    document.execCommand()的用法小记 一.语法 execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令.处理Html数据时常用. 如下格式:document.ex ...