Implement strStr() leetcode java
题目:
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
题解:
其实我觉得这题。。为啥不给个更明确的解释呢?
是不是如果不知道strStr()是干嘛的就给直接挂了呢。。。
这道题就是让你判断,needle是不是haystack的子串,是的话就返回这个子串。
解题想法是,从haystack的第一个位置,开始逐个判断是不是子串。如果整个子串都匹配了,那么就返回,否则继续往下挪位置。
注意要看haystack剩余的长度跟needle比足不足够多,不够的话也就不用往后比了。
写到这突然想起来这个不就是《数据结构》那本书里面那个例子么,这应该是最naive的解法,之后讲的就是kmp解法,可以往后滑动的那种。。。
了解kmp算法网上应该有很多教程,之前是看严蔚敏老师的视频学习的,老师讲的很细,拿着小纸片当指针一个一个指着给你讲,很清楚。。。对我这种理解能力慢的人就恨受用了。。。
我这个就不是kmp了,就最naive的方法。
代码如下:
1 public String strStr(String haystack, String needle) {
2 if (needle.length() == 0)
3 return haystack;
4
5 for (int i = 0; i < haystack.length(); i++) {
6 if (haystack.length() - i + 1 < needle.length())
7 return null;
8
9 int k = i;
int j = 0;
while (j < needle.length() && k < haystack.length() && needle.charAt(j) == haystack.charAt(k)) {
j++;
k++;
if (j == needle.length())
return haystack.substring(i);
}
}
return null;
}
Reference:http://www.programcreek.com/2012/12/leetcode-implement-strstr-java/
public int strStr(String haystack, String needle) {
for (int i = 0; ; i++) {
for (int j = 0; ; j++) {
if (j == needle.length()) return i;
if (i + j == haystack.length()) return -1;
if (needle.charAt(j) != haystack.charAt(i + j)) break;
} }
}
Implement strStr() leetcode java的更多相关文章
- Implement strStr() [LeetCode]
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- Java for LeetCode 028 Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- [Leetcode] implement strStr() (C++)
Github leetcode 我的解题仓库 https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- Leetcode 28——Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
随机推荐
- Webpack中的css-loader 和style-loader
传统上我们会在html文件中引入CSS代码,借助webpack style-loader和css-loader我们可以在.js文件中引入css文件并让样式生效. style-loader和css-lo ...
- 洛谷 P4427 求和
传送门啦 思路: 开始不肿么容易想到用倍增,但是想到需要求 $ Lca $ ,倍增这种常数小而且快的方法就很方便了.求 $ Lca $ 就是一个最普通的板子.那现在考虑怎么求题目中的结果. 树上差分可 ...
- python 全栈开发,Day75(Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件)
昨日内容回顾 基于对象的跨表查询 正向查询:关联属性在A表中,所以A对象找关联B表数据,正向查询 反向查询:关联属性在A表中,所以B对象找A对象,反向查询 一对多: 按字段:xx book ----- ...
- GDIPlus非典型误用一例
// ** 初始化GDI+ Gdiplus::GdiplusStartupInput gdiplusStartupInput; // ** 该成员变量用来保存GDI+被初始化后在应用程序中的GDI+标 ...
- Jsp运行原理
当客户端浏览器向服务器发出访问一个JSP页面的请求时,服务器根据该请求加载相应的JSP页面,并对该页面进行编译,然后执行.JSP页面的执行过程如下图所示: 客户端通过浏览器向服务器发出请求,在该请求中 ...
- sed & awk之sed
sed处理文本的方法 sed在处理文本时,会先读取第一个输入行,将编辑命令应用于输入行,然后读取下一个输入行,并应用编辑命令.sed总是处理最新版本的行,因此sed中有多个编辑命令时,编辑命令的顺序对 ...
- URAL - 1078 Segments
URAL - 1078 题目大意:有n条线段,一个线段a 完全覆盖另一个线段b 当且仅当,a.l < b.l && a.r>b.r.问你 一个线段覆盖一个线段再覆盖一个线段 ...
- 移动端js触摸事件大全
一.手机上的触摸事件 基本事件: touchstart //手指刚接触屏幕时触发 touchmove //手指在屏幕上移动时触发 touchend //手指从屏幕上移开时触发 下面这 ...
- java里获取uuid
//获取32位uuid工具类 ,此类事java自带的,不需要导包public static String get32UUID() { String uuid = UUID.randomUUID().t ...
- VSCode tasks.json中的各种替换变量的意思 ${workspaceFolder} ${file} ${fileBasename} ${fileDirname}等
When authoring tasks configurations, it is often useful to have a set of predefined common variables ...