Problem : 实现找子串的操作:如果没有找到则返回-1  ,如果找到则返回第一个匹配位置第一个字符的下标
 
遍历操作,依次遍历子串中的元素,从长串的第i个元素位置开始,当成功遍历结束子串所有元素时,返回此时的i即时所求结果,
如果此时的i+j已经等于长串的个数,那么表示没有找到,返回-1 
 
参考代码:
package leetcode_50;

/***
*
* @author pengfei_zheng
* 实现找子串的功能
*/
public class Solution28 {
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;
}
}
}
}

LeetCode 28 Implement strStr() (实现找子串函数)的更多相关文章

  1. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

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

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

  3. [LeetCode] 28. Implement strStr() 解题思路

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

  4. [LeetCode] 28. Implement strStr() ☆

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

  5. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  6. Java [leetcode 28]Implement strStr()

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

  7. Leetcode 28——Implement strStr()

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

  8. [leetcode]28. Implement strStr()实现strStr()

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

  9. LeetCode——28. Implement strStr()

    题目: class Solution { public: int strStr(string haystack, string needle) { if(needle.empty()){ return ...

随机推荐

  1. C# 实现写入文本文件内容功能

    private void write_txt(string str1, string str2, string str3) { System.DateTime currentTime = System ...

  2. HDU 3970 Harmonious Set 容斥欧拉函数

    pid=3970">链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n  求连续整数[0,n), 中随意选一些数使得选出的 ...

  3. Node.js之接收前台数据实例

    <form class="form-horizontal" method="post" action="http:127.0.0.1:3000/ ...

  4. GitHub 二次验证收不到短信咋办?

    身在天朝,用了国外的代码托管服务,会有些烦恼的. 网速慢就不说了,如果启用了二次验证,短信收不到那就悲催了. 之前的都能收到短信的,突然间尝试了很多天都不行,联系github的客服,几次的答复如下: ...

  5. 【转】ZooKeeper详细介绍和使用第一节

    一.分布式协调技术 在给大家介绍ZooKeeper之前先来给大家介绍一种技术——分布式协调技术.那么什么是分布式协调技术?那么我来告诉大家,其实分布式协调技术 主要用来解决分布式环境当中多个进程之间的 ...

  6. 【java】 java SPI

    SPI(Service provider interface)是旨在由第三方实现或扩展的API.它可以用于启用框架扩展和可替换组件. 服务是一组众所周知的接口或(通常是抽象的)类.服务提供者是服务的特 ...

  7. python--内置函数---13

    原创博文,转载请标明出处--周学伟 http://www.cnblogs.com/zxouxuewei/ Python 内置函数     内置函数     abs() divmod() input() ...

  8. eclipse 安装 ndk 组件

    新安装的eclipse没有ndk组件, 我使用的安装包是:android-ndk32-r10b-windows-x86_64.zip,打开preferences如下

  9. 【Python】Linux Acanoda PySpark Spark

    1.安装 Acanoda  2.安装 Spark和Scala 3.安装 PySpark 4.将Spark的Python目录拷贝至 Acanoda目录下 5.安装py4j,切换anaconda中bin目 ...

  10. 使用HTML5监测网站性能

    在这个信息爆炸的互联网时代,越来越多的人缺少了等待的耐心,网站性能对于一个网站来说越来越重要.以下为监控到的网站打开时间对跳出率的影响: 当网站打开时间在0-1秒时,跳出率为12% 当网站打开时间在1 ...