题目

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. python for dl

    算是python的简明教程吧,总结的不错: https://zhuanlan.zhihu.com/p/24162430 python for opencv: https://zhuanlan.zhih ...

  2. Tronado

    Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了能有效 ...

  3. CCF2016093炉石传说(C语言版)

    问题描述 <炉石传说:魔兽英雄传>(Hearthstone: Heroes of Warcraft,简称炉石传说)是暴雪娱乐开发的一款集换式卡牌游戏(如下图所示).游戏在一个战斗棋盘上进行 ...

  4. codeforce 139E

    成段更新+离散化才能过,数据好强.. 单点更新挂在了test27,下次做到成段更新再来做! /* 期望=存活概率*点权值/100 ans=sum(期望) 离散化树木权值,数轴统计累加可能倒下的树木概率 ...

  5. 使用Struts,实现简单的登录

    一.新建项目Struts 1.右键 new————Web Project 2.点击项目——右键——myeclipse——add Struts Capabilities.....——选择struts2. ...

  6. python全栈开发day32-进程创建,进程同步,进程间的通信,进程池

    一.内容总结 1.进程创建 1) Process:两种创建一个新进程的方法: 1.实例化Process,通过args=(,)元组形式传参,2创建类继承Process,类初始化的时候传参数 2) p.j ...

  7. zabbix 检测icmp参数

    UserParameter=ICMPresult,ping -c 4 10.128.1.22 &> /dev/null;echo $?

  8. 在phpstorm中svn的使用

    目 录 1.搭建svn环境 1.1搭建svn服务端  1.2创建svn用户和密码 2.开始在phpstorm中链接svn 2.1打开Subversion 2.2 输入svn地址 2.3选择导出文件,进 ...

  9. HDU1211 密文解锁 【扩展欧几里得】【逆元】

    <题目链接> <转载于 >>> > 题目大意: RSA是个很强大的加密数据的工具,对RSA系统的描述如下: 选择两个大素数p.q,计算n = p * q,F( ...

  10. Solution for unable to create "dead-letter-exchange" in RabbitMQ

    在参考 Dead-Letter-Exchange 进行Dead-letter-exchange的理解, 在本地时,想要创建 Dead-letter-exchange 时,一直报错,错误如下: Unha ...