题目

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的更多相关文章

  1. Implement strStr() [LeetCode]

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  2. Java for LeetCode 028 Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  4. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  5. [LeetCode] 28. Implement strStr() 实现strStr()函数

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

  7. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  8. Leetcode 28——Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

随机推荐

  1. java uitl

    Random类  //生成随机数 https://i.cnblogs.com/EditPosts.aspx?opt=1

  2. Gym100340 线性dp

    //看题解写的 https://blog.csdn.net/sdfzyhx/article/details/51804748#include<bits/stdc++.h> using na ...

  3. python 全栈开发,Day36(作业讲解(大文件下载以及进度条展示),socket的更多方法介绍,验证客户端链接的合法性hmac,socketserver)

     先来回顾一下昨天的内容 黏包现象粘包现象的成因 : tcp协议的特点 面向流的 为了保证可靠传输 所以有很多优化的机制 无边界 所有在连接建立的基础上传递的数据之间没有界限 收发消息很有可能不完全相 ...

  4. Math对象的常用属性和方法

    属性 描述 Math.PI 返回π(3.1415926) 方法 描述 Math.round() 将数字四舍五入到离它最近的整数 Math.sart(n) 返回平方根,例如Math.sart(9)返回3 ...

  5. asp.net core 2.0 cookie的使用

    本文假设读者已经了解cookie的概念和作用,并且在传统的.net framework平台上使用过. cookie的使用方法和之前的相比也有所变化.之前是通过cookie的add.set.clear. ...

  6. Kubernetes 安装

    一.引用自:https://www.cnblogs.com/gaoyuechen/p/8685771.html#_label6 二.安装过程中docker 坑 转自:https://blog.csdn ...

  7. java基础面试题-2

    第一,谈谈final, finally, finalize的区别.  final---修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此一个类不能既被 ...

  8. 005 Spark快速入门的简单程序案例

    参考:官网的quick start http://spark.apache.org/docs/1.6.0/quick-start.html 这里只是在shell命令行中简单的书写一些命令,做一个简单的 ...

  9. Python安装scrapy过程中出现“Failed building wheel for xxx”

    https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml Python安装scrapy库过程中出现“ Failed building wheel for xxx ...

  10. char *s 与 char s[ ]的区别

    程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其 操作方式类似于数据结构中的栈. 2.堆区( ...