LeetCode28.实现strStr()
实现 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) {
if (needle.length() == 0) {
return 0;
}
for (int i=0;i<=haystack.length()-needle.length();i++) {
if (haystack.subSequence(i, needle.length()+i).equals(needle)) {
return i;
}
}
return -1;
}
}
LeetCode28.实现strStr()的更多相关文章
- LeetCode28 Implement strStr()
题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...
- [Swift]LeetCode28. 实现strStr() | Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- leetcode-28.实现strStr()
leetcode-28.实现strStr() 题意 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字 ...
- LeetCode28.实现strStr() JavaScript
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- LeetCode28——实现strStr()
6月中下旬辞职在家,7 月份无聊的度过了一个月.8 月份开始和朋友两个人写项目,一个后台和一个 APP ,APP 需要对接蓝牙打印机.APP 和蓝牙打印机都没有搞过,开始打算使用 MUI 开发 APP ...
- leetcode28 strstr kmp bm sunday
字符串匹配有KMP,BM,SUNDAY算法. 可见(https://leetcode-cn.com/problems/implement-strstr/solution/c5chong-jie-fa- ...
- [PHP源码阅读]strpos、strstr和stripos、stristr函数
我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- strstr 函数的实现
strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...
随机推荐
- xCode 升级9.3之后巨卡
因为项目要适配iPhone8, iPhoneX 等.需要升级Xcode需要升级到9.3.但是 MAC系统是10.12的,需要升级到10.13. 系统升级完之后升级Xcode.之后Xcode 就各种卡. ...
- 如何实现浏览器向服务器伪造refer?
Request URL:https://www.getwnmp.org/ Request Method:GET Status Code:304 Remote Address:45.55.134.251 ...
- php安全
1.会话安全性 会话固化 一种获取有效回话标识符的方法,他将运行恶意用户通过强制使用回话ID来轻松模拟一个真实用户 攻击方法:<a href="http://a.com/index.p ...
- Scaleform 中的 3D视角相关研究
参考文献: 1.D3D中的第一人称视角 2.透视投影的原理和实现 http://blog.csdn.net/ww51xh/article/details/2910 3.深入探索透视投影变换 http: ...
- pandas网页操作基础
ipython notebook 命令行输入ipython notebook 此时,浏览器会自动运行并打开ipython网页 基本操作 如上图所示,新建一个项目 导入相关模块,建立一个数据集 制造数据 ...
- sqlserver配置允许快照隔离
ALTER DATABASE TustenaOS SET ALLOW_SNAPSHOT_ISOLATION ON
- 'react-scripts' is not recognized as an internal or external command
React项目在执行npm start的时候报下面的错误: 解决办法:把项目目录中node_modules文件夹删掉,重新npm install一下,然后再执行npm start
- C#生成exe、dll版本号自动增加
修改AssemblyInfo.cs 1.注释[assembly: AssemblyFileVersion("1.0.0.0")] 2.[assembly: AssemblyVers ...
- (4.28)for xml path 在合并拆分上的作用演示
for xml path 用于合并与拆分 1.合并 很多时候需要在SQL Server中创建逗号分隔列表.这可以使用SQL Server的DOR XML PATH功能完成.与select语句一起使用时 ...
- SELinux介绍
SELinux概念 安全加强的Linux,早期的Linux系统安全由系统管理员控制.SELinux就是一些安全规则的集合,类似于人类生活中的法律. DAC: 自由访问控制(以前的linux版本) ...