题目

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. TP5.1:request请求对象(使用四种方式获取)

    准备: 在index/controller下创建一个名为requests.php的文件(注意:不要起名为request,因为它是关键字,不被允许起名) 动态方法和静态方法的区别: 静态方法:publi ...

  2. VS2008/2005 MFC程序调试经验

    我的VS2008不知道是有bug还是自己的问题,很多时候变量定义后CTRL+F5运行却没反应,一定要“生成解决方案”下才行? 1.没有可用于当前位置的源代码 将工具->选项->调试-> ...

  3. python 全栈开发,Day112(内容回顾,单例模式,路由系统,stark组件)

    一.内容回顾 类可否作为字典的key 初级 举例: class Foo(object): pass _registry = { Foo:123 } print(_registry) 执行输出: {&l ...

  4. 20155309南皓芯 网络对抗《网络攻防》 Exp1 PC平台逆向破解(5)M

    实践目标 本次实践的对象是linux的可执行文件 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShell,会返回一个可 ...

  5. jvm类加载器以及双亲委派

    首先来了解几个概念: 类加载: 概念:虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验--转换解析--初始化,最终形成能被java虚拟机直接使用的java类型,就是jvm的类加载机制. ...

  6. HTML5游戏 围住神经猫 开发

    所有文章搬运自我的个人主页:sheilasun.me 去年风靡微信朋友圈的小游戏"围住神经猫",我也试着做了一下,可以戳这里试玩→围住神经猫.游戏是用Egret引擎开发的,因为Eg ...

  7. Asp.Net Core 2.0 项目实战(5)Memcached踩坑,基于EnyimMemcachedCore整理MemcachedHelper帮助类。

    Asp.Net Core 2.0 项目实战(1) NCMVC开源下载了 Asp.Net Core 2.0 项目实战(2)NCMVC一个基于Net Core2.0搭建的角色权限管理开发框架 Asp.Ne ...

  8. Dockerfile中ENTRYPOINT 和 CMD的区别

    一.dockerfile中的 CMD 1.每个dockerfile中只能有一个CMD如果有多个那么只执行最后一个. 2.CMD 相当于启动docker时候后面添加的参数看,举个简单例子: docker ...

  9. 用HTML+CSS画出一个同心圆

    参加web前端校招的同学们经常会遇到这样的面试题:用HTML+CSS画出一个同心圆. 例如: 这道题主要考验的是基础盒模型布局能力和倒圆角属性的巧用. 1.html代码 <body> &l ...

  10. 洛谷P2347 砝码称重 【多重背包】(方案数)(经典)

    题目链接:https://www.luogu.org/problemnew/show/P2347 题目描述 设有1g.2g.3g.5g.10g.20g的砝码各若干枚(其总重<=1000), 输入 ...