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. Windows环境下ELK(5.X)平台的搭建

    一.Windows环境下ELK平台的搭建(2.*) 1.安装配置Java环境 在Oracle官网获取最新版的Java版本,由于只是运行不是开发,所以也可以只下载JRE.官网:http://www.or ...

  2. linux中kill命令

    Linux中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以使用Ctrl+C键,但是,对于一个后台进程就须 ...

  3. TensorFlow:tf.contrib.layers.xavier_initializer

    xavier_initializer( uniform=True, seed=None, dtype=tf.float32 ) 该函数返回一个用于初始化权重的初始化程序 “Xavier” .这个初始化 ...

  4. Linux 系统目录介绍

    bin : bin 是Binary 二进制的缩写,就是可执行文件了.Bin目录下是用户常用的命令. sbin: 此目录下也是二进制文件 ,不过这里的命令是 超级用户如 root 这样的用户使用的. e ...

  5. 微信小程序/支付宝小程序 WxParse解析富文本(html)代码

    小程序本身并不太支持html代码,比如html的img.span.p这个时候改这么办呢?需要用到一个小插件WxParse来实现. 小程序高级交流群:336925436  微信小程序支持富文本编辑器代码 ...

  6. swift闭包的另一种用法

    这不是教程. 当你碰到函数参数需要传递一个闭包(closure)时,一般是可以直接这么传递的(假定无返回): // 教程一般教你在参数位置传递closure: someMethod(arg1, arg ...

  7. 【spark】with mongodb

    https://docs.mongodb.com/spark-connector/master/python-api/

  8. FormData 对象的使用

    FormData 对象的使用 在本文章中 如何创建一个FormData对象 通过HTML表单创建FormData对象 使用FormData对象上传文件 通过AJAX提交表单和上传文件可以不使用Form ...

  9. jQuery实现自动调用和触发某个事件的方法

    1.比如我们通过jquery定义了一个点击事件,我们如何自动触发他: $(function(){    $('#button').click(function(){      alert('butto ...

  10. 分析技术在PMP中的应用

    Analytical Techniques, 根据可能的项目或者环境变量变化以及它们与其他变量之间的关系,对潜在后果进行评估,分析和预测的各种技术. 4.4.2.2 监控项目工作: 分析技术 包括: ...