LeetCode--028--实现strStr() (java)
实现 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 字符串出现的第一个位置 ...
- [LeetCode]28.实现strStr()(Java)
原题地址: implement-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 ...
随机推荐
- PHP----------php封装的一些简单实用的方法汇总
1.xml转换成array,格式不对的xml则返回false function xml_parser($str){ $xml_parser = xml_parser_create(); i ...
- 前端学习历程--http与https
一.CA(证书授权中心)证书 1.ca是通信的中介,具有足够的权威性 2.信任可嵌套如:C 信任 A1,A1 信任 A2,A2 信任 A3 二.根本区别 1.https需要基于ssl的ca证书认证(判 ...
- ELK简单安装
ELK日志分析平台 一.ELK介绍 ELK是三个开源软件的缩写,分别为:Elasticsearch . Logstash以及Kibana,都是开源软件,新增一个beats,(轻量级日志处理工具Agen ...
- Vue系列之 => webpack处理样式文件
处理css文件 安装 npm i style-loader css-loader -D main.js import $ from 'jquery' //Es6中导入模块的方式 import './c ...
- springmvc <mvc:default-servlet-handler/> & <mvc:annotation-driven>
1. mvc 标签内部加载的基础类: 1). <mvc:view-controller> 1 org.springframework.web.servlet.handler.SimpleU ...
- maven配置国内阿里云镜像
<mirrors> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> ...
- react小知识点
1.render函数什么时候会执行? 当this.state/this.props发生改变的时候render函数就会执行 2.组件第一次执行的时候会执行哪些生命周期 constructor--> ...
- JS对象格式化方法:pretty_format
/* * 格式化 * */ var pretty_format = function (obj, indent) { if (obj === null) return 'null'; if (obj ...
- 父级POM的表现形式
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- Vue 旅游网首页开发3 - Ajax获取首页数据
之前的首页数据都是写死在页面上的,现在修改项目,使得数据通过ajax动态获取. 死胎了 ... 不想写了····