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 ...
随机推荐
- python 全栈开发,Day79(Django的用户认证组件,分页器)
一.Django的用户认证组件 用户认证 auth模块 在进行用户登陆验证的时候,如果是自己写代码,就必须要先查询数据库,看用户输入的用户名是否存在于数据库中: 如果用户存在于数据库中,然后再验证用户 ...
- 步步为营-71-asp.net的简单练习(图片处理)
1 原有图片添加水印 1.1 封装一个类,用于获取文件路径 using System; using System.Collections.Generic; using System.IO; using ...
- Ext.js中自己扩展的EasyGrid
这里只写了一些核心的代码,具体如下: Ext.ux.EasyGrid = Ext.extend(Ext.grid.GridPanel, { initComponent: function () { t ...
- 检查Rabbitmq中队列及消息个数,还有清空的方法
rabbitmqctl list_queues rabbitmqctl purge_queue kafka_queue (不推荐)使用reset方式重置rabbitmq即清空掉rabbitmq所有的队 ...
- C# mongoDB Driver 使用对象方式查询语法大全
#region 查询方法 /// <summary> /// 获取单个对象 /// </summary> /// <typeparam name="T" ...
- 【LOJ】#2187. 「SHOI2014」三叉神经树
题解 可以发现每次修改的是这个点往上一条连续的链,如果我要把1改成0,需要满足这一段往上的一部分都有两个1 如果我要把0改成1,需要满足这一段往上的部分有两个0 对于每个点记录1的个数,发现我们只会把 ...
- 菜单联动,select联动菜单(搜索之后默认选中)
框架:thinkphp php控制器代码: $schedulelist = M('schedule')->getField('sid,schedule_name'); $this->ass ...
- java基础面试题-2
第一,谈谈final, finally, finalize的区别. final---修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此一个类不能既被 ...
- 《Gradle权威指南》--Android Gradle多项目构建
No1: Android多项目设置 目录结构: MyProject/ setting.gradle app/ build.gradle libraries/ lib1/ build.gradle li ...
- 为什么macos开机黑屏但是有声音?
一般使用时间久了,或者升级系统就会出现这个情况.完全关机,然后按开机键,再按command+option+R+P.不要松手,等重启几次,响几次(3次左右).然后松手等它自己重启即可. mac开机黑 ...