实现 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)的更多相关文章
-
Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
-
LeetCode 028 Implement strStr()
题目要求:Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in h ...
-
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 + ...
-
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 ...
-
LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
-
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 ...
-
前端与算法 leetcode 28.实现 strStr()
# 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...
-
Java实现 LeetCode 28 实现strStr()
28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...
-
Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
-
【LeetCode】028. Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
-
Java 集合类 TreeSet、TreeMap
TreeMap和TreeSet的异同: 相同点: TreeMap和TreeSet都是有序的集合,也就是说他们存储的值都是拍好序的. TreeMap和TreeSet都是非同步集合,因此他们不能在多线程之 ...
-
[Leetcode] Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
-
jQuery基础DOM和CSS操作
$('#box').html();//获取 html 内容$('#box').text();//获取文本内容,会自动清理 html 标签$('#box').html('<em>www.li ...
-
NServiceBus教程-消息传递与处理
nservicebus"的容错默认"设计的一部分,基础设施管理事务自动所以你不需要记住所有的线程和状态管理要素配置. 客户端和服务器 理想情况下,服务器代码处理消息事务,但它往往不 ...
-
Enter键提交表单
input type="submit"在360浏览器上不能提交 用了这个 <input type="button" class="btn b ...
-
ecos的app生命周期
5种变迁,安装.更新.启动.暂停.卸载,每个app都可以自行维护每种变迁 方法简单,只需要在app/$app_name目录下定义task.php文件 <?php class desktop_ta ...
-
Backbone视图渲染React组件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
-
python 之 列表list &;&; 元组tuple
目录: 列表 列表基本操作 列表的操作符 列表的函数和方法 元组 介绍: 列表是一种可变的有序集合,可以进行访问.添加和删除操作. 元组是一种不可变的有序集合,可以访问. 1.列表的基本操作 创建列表 ...
-
Python3学习笔记06-字符串
可以使用引号('或")来创建字符串. var1 = 'Hello World!' var2 = "Runoob" 在最新的Python 3版本中,字符串是以Unicode ...
-
JavaScript:document.execCommand()的用法
document.execCommand()的用法小记 一.语法 execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令.处理Html数据时常用. 如下格式:document.ex ...