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. 微信小程序开发1_资料收集

    [前言] 小程序 [一.资料] 微信官网 开发文档.工具 等 https://mp.weixin.qq.com/cgi-bin/wx [二] 创建小程序和编辑代码,先安装 开发者工具 ,根据所使用的操 ...

  2. 限定某个目录禁止解析php 限制user_agent php相关配置

  3. VOIP NAT穿越之SIP信令穿越

    本文原创自 http://blog.csdn.net/voipmaker  转载注明出处. 本文是VOIP通信NAT系列专题的第三篇, 本文论述NAT对SIP协议穿越的影响.SIP协议是基于文本的,而 ...

  4. linux 系统安装mysql (rpm)

    其实按照本文安装成功,但是启动依然有问题:最好参考链接配置. http://blog.csdn.net/xiaoxiaoxuewen/article/details/7550107 我用的是ubunt ...

  5. 深入浅出:全面理解SQL Server权限体系

    转自IT168  好文转载存档! [IT168 技术]权限两个字,一个权力,一个限制.在软件领域通俗的解释就是哪些人可以对哪些资源做哪些操作.在SQL Server中,"哪些人", ...

  6. webpack学习简单总结

    webpack使用总结: 入门使用: 这个报错说明需要安装相应的Loader,并在引用时指定相应的loader 执行成功如图: chunk指相应的区块. 要是css引入正确:必须引入css-loade ...

  7. libstdc++.so.5: undefined reference to `memcpy@GLIBC_2.14'

    没有别的原因: 找正确的 libstdc++.so.5 包就成. 我这儿有,需要的可以下载奥!

  8. maven package 与maven install的区别

    maven package:会将jar包打包到target下 maven install:将jar包装载到maven仓库,供其他项目使用 项目基于osgi开发的,打包有依赖关系,依赖关系主要是在pom ...

  9. Java -- 异常的捕获及处理 -- 目录

    7 异常的捕获及处理 7.1 异常的基本概念 7.1.1 为什么需要异常处理 7.1.2 在程序中使用异常处理 7.1.3 异常类的继承结构 7.1.4 Java的异常处理机制 7.2 throws与 ...

  10. 8 -- 深入使用Spring -- 3...2 ResouceLoader 接口和 ResourceLoaderAware 接口

    8.3.2 ResouceLoader 接口和 ResourceLoaderAware 接口 Spring 提供如下两个标志性接口: ⊙ ResourceLoader : 该接口实现类的实例可以获得一 ...