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;
10         int j = 0;
11  
12         while (j < needle.length() && k < haystack.length() && needle.charAt(j) == haystack.charAt(k)) {
13             j++;
14             k++;
15             if (j == needle.length())
16                 return haystack.substring(i);
17         }
18  
19     }
20     return null;
21 }

Reference:http://www.programcreek.com/2012/12/leetcode-implement-strstr-java/

Implement strStr() leetcode java

时间: 2024-08-01 02:17:50

Implement strStr() leetcode java的相关文章

Implement strStr() Leetcode

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 真是要崩溃了...这道题好像是第三遍做了还是做不利索...= = public class Solution { public int strStr(String haystack, String needle) { if (haystack ==

leetcode Implement strStr()(easy) /java

我以为,当时我用c++写这个函数的时候,整个人如同乱麻. 这次用java写.先查的SE 8中String的方法.找到两个与此函数有关的方法:matches()和substring(). import java.io.*; import java.util.*; public class Solution { public static int strStr(String haystack, String needle) { int r=-1; int len1=haystack.length()

Java for LeetCode 028 Implement strStr()

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 解题思路: 直接看代码即可,JAVA实现如下: static public int strStr(String haystack, String needle) { for(int i=0;i<=haystack.length()-needle.l

[LeetCode][Java] Implement strStr()

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 题意: 实现strStr() 返回needle在haystack中第一次出现的位置,如果haystack中不存在needle,返回-1. 算法分析: 方法一: * in Java, there is an API function name

LeetCode 28 Implement strStr() (C,C++,Java,Python)

Problem: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If

Java [leetcode 28]Implement strStr()

题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If you

[LeetCode] 028. Implement strStr() (Easy) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 028. Implement strStr() (Easy) 链接: 题目:https://oj.leetcode.com/problems/implement-strstr/ 代码(github):https://github.com/illuz/leetcode 题意: 在一个字符串里找另一个字符串在其中的位置

LeetCode 28. Implement strStr()

https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 字符串简单题.一定要写的没BUG. 如果不在函数最后一行写return语句的话,LeetCode会出RUNTIME ERROR. Line 27: co

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n