题目

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. js有关事件驱动

    事件驱动               /*                 什么是事件?                 1.事件发生了                 2.我要对这个事件做对应的处理 ...

  2. Code alignment 代码对齐改进(VS2017)

    In mathematics you always keep your equals lined up directly underneath the one above. It keeps it c ...

  3. POJ 2385 Apple Catching【DP】

    题意:2棵苹果树在T分钟内每分钟随机由某一棵苹果树掉下一个苹果,奶牛站在树#1下等着吃苹果,它最多愿意移动W次,问它最多能吃到几个苹果.思路:不妨按时间来思考,一给定时刻i,转移次数已知为j, 则它只 ...

  4. thinkphp搭建后台品字形框架页面

    页面分为三个部分 head,left,right共同组成了index 在indexController中 function Index(){ $this->display(); } //展现后腰 ...

  5. Lemur编写索引器

    http://blog.sciencenet.cn/blog-273829-312138.html http://sourceforge.net/p/lemur/wiki/Home/ http://q ...

  6. 转:CentOS下后台运行Python脚本及关闭脚本的一些操作

    自己写了一个python脚本,但是直接远程用putty连接后#python xxx.py执行,关闭putty脚本也随之关闭了,这里需要用到‘setsid’这个命令. #setsid python xx ...

  7. P1541 乌龟棋 线性dp

    题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行NN个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第NN格是终点,游戏要求玩家控制一个乌龟棋子 ...

  8. 最长上升序列 LCS LIS

    子序列问题 (一)一个序列中的最长上升子序列(LISLIS) n2做法 直接dp即可: ;i<=n;i++) { dp[i]=;//初始化 ;j<i;j++)//枚举i之前的每一个j ) ...

  9. 【Java】 剑指offer(29) 顺时针打印矩阵

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字. 思 ...

  10. Redis工具类

    /** * Copyright © 2012-2016 * <a href="https://github.com/thinkgem/smkj">smkj</a& ...