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 ...
随机推荐
- 一个简单小技巧实现手机访问.html文件网页效果
注册登录Github不解释 settings设置往下拉 选择一个主题上传代码文件到code 打开这个文件选择此时的网址 在网址前面加上 这段代码 http://htmlpreview.github.i ...
- test request&&response 代码实现
使用工具 IDEA 创建一个登录页面和后方数据库连接 1.编写login.html文件 导入到web文件夹下 设置配置文件 druid.properties 导入jar包 放置到web文件夹下 ...
- Entity framework 绑定到Datagridview的添加删除修改
Entity framework 绑定到Datagridview的添加删除修改 using System; using System.Collections.Generic; using System ...
- Python批量合并处理B站视频
最近想学习后端,又不想花钱,怎么办呢?于是在手机端B站(哔哩哔哩)上面找到了满意的免费视频教程,但是手机端看起来很不方便啊.于是,我通过在手机端缓存下来后,导入到了电脑端,但是我后面了发现两个问题: ...
- 用iPhone查看pc电脑上写的html(Mac电脑Charles)简单版
对于客户端同学开发来说,写一段代码想在真机上看看,是非常容易的. 那么在这么一个大前端的环境下,客户端开发想写点html和js代码,又想在手机上看看效果,怎么办呢? 需要以下几个步骤: 大体流程:1. ...
- Linux中LAMP构架的实现
LAMP:Linux+Apache+Mysql+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度共同组 ...
- 这个表明将http协议转成websocket协议
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- sehll 小脚本的编写{基础}
1.模拟linnux登录shell #/bin/bash echo -n "login:" read name echo -n "password:" read ...
- windows系统开启虚拟化
电脑如何开启虚拟进化 当VMware安装系统时,如果电脑没有开启虚拟化,在安装过程中,会发生错误. 查看电脑是否开启虚拟化,[任务管理器] 正常情况如上图,而当虚拟化没有开启的时候,如何解决? int ...
- 【题解】Luogu P4069 [SDOI2016]游戏
原题传送门 看到这种题,想都不用想,先写一个树链剖分 然后发现修改操作增加的是等差数列,这使我们想到了李超线段树 先进性树剖,然后用李超线段树维护区间最小,这样就做完了(写码很容易出错) 复杂度为\( ...