题目

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. poj2019 二维RMQ模板题

    和hdu2888基本上一样的,也是求一个矩阵内的极值 #include<iostream> #include<cstring> #include<cstdio> # ...

  2. 性能测试十四:Xshell链接linux虚拟机

    一.先装一个linux虚拟机 VBox+centos1.先下载Linux镜像文件的ovf或者OVA文件2.打开vbox,点击菜单栏“管理”-“导入虚拟电脑3.选择解压路径中的ovf或者OVA文件,点击 ...

  3. String中的toCharArray:将此字符串转换为新的字符数组,并统计次数

    package stringyiwen; public class StringTestToCharArray { public static void main(String[] args) { S ...

  4. docker安装sonarqube及实际应用

    由于平台的多样化,在不同环境的安装方式可能也不一样,为了避免环境不一致带来的差异,特记一笔容器安装: 一.Sonar可以从以下七个维度检测代码质量,而作为开发人员至少需要处理前5种代码质量问题. 1. ...

  5. bootstrap 全局样式设置

    HTML <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" co ...

  6. stl 常用代码

    CString类型的replace ; while((pos = it->m_strFile.find(_T("%UC_INSTALL_ROOT%\\"), pos)) != ...

  7. Redis的优势和特点

    Redis的特点: 内存数据库,速度快,也支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用. Redis不仅仅支持简单的key-value类型的数据,同时还提供list ...

  8. Python 爬取生成中文词云以爬取知乎用户属性为例

    代码如下: # -*- coding:utf-8 -*- import requests import pandas as pd import time import matplotlib.pyplo ...

  9. [OpenCV-Python] OpenCV 中的图像处理 部分 IV (五)

    部分 IVOpenCV 中的图像处理 OpenCV-Python 中文教程(搬运)目录 22 直方图 22.1 直方图的计算,绘制与分析目标 • 使用 OpenCV 或 Numpy 函数计算直方图 • ...

  10. Codeforces 862B (二分图染色)

    <题目链接> 题目大意: 给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且此图还是一个二分图. 解题分析: 此题不难想到,假设二分图点集数 ...