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 ...
随机推荐
- from opencv image to PIL image and reverse
import cv2 import numpy as np from PIL import Image img = cv2.imread("path/to/img.png") # ...
- ambari下的flume和kafka整合
1.配置flume #扫描指定文件配置 agent.sources = s1 agent.channels = c1 agent.sinks = k1 agent.sources.s1.type=ex ...
- listbox或datagrid内容双击事件绑定
====listbox=== <DataTemplate> <Border Width="350" Height="230" Margin=& ...
- 【svn】svn的使用
直接看这个教程:http://www.runoob.com/svn/svn-tutorial.html 里面讲得很有条理,很容易看懂.提供了指令以及相应的名词解释.不像其他网上的资料,干巴巴的只有一对 ...
- 查看Linux端口的占用及连接情况
[root@cloudplatform ~]# netstat -nap | grep 22066 | grep 127.0.0.1 -vtcp 0 0 :::22066 ...
- [HDU] 5306 Gorgeous Sequence [区间取min&求和&求max]
题解: 线段树维护区间取min求和求max 维护最小值以及个数,次小值 标记清除时,分情况讨论 当lazy>max1 退出 当max1>lazy>max2(注意不要有等号) 更新 否 ...
- day8--socket回顾
后面学习了线程.协成和异步,它们的框架都是基于socket的协议,基本原理都是一样的,现在把这几个模块重温一下,尽量掌握这些知识更全面一些. 动态导入模块,知道知道模块名,可以像反射一样,使用字符串来 ...
- 【AtCoder】CODE FESTIVAL 2017 qual A
A - Snuke's favorite YAKINIKU -- #include <bits/stdc++.h> #define fi first #define se second # ...
- 文件流 io.StringIo()
import io f = io.StringIO() f.write("") f.getvalue() f.close 二进制 f = io.Bytesio()
- FastAdmin 的 CRUD 不支持层级模型
FastAdmin 的 CRUD 可以快速生成控制器,模型和前端文件. 群里有人试了这个命令: php think crud -t departmant -c auth/departmant -m ...