Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

注:直接看题目可能会有些偏差,因为容易认为 needle是一个字符,那就也太容易了,实则,haystack和needle都是字符串(注意是字符串不是数组)

解法思路:

暴力搜索:

public class Solution {
  public int strStr(String haystack, String needle) {
    int i=0,j=0;
    while(i<haystack.length()&&j<=(needle.length()-1))
    {
      if(haystack.charAt(i)==needle.charAt(j))
      {
        i++;
         j++;
      }
      else
      {
        i=i-j+1;
        j=0;
      }
    }
    if(j==needle.length())
      return i-j;
    else return -1;
  }
}

感受下 clean code

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 详解(Implement strstr)的更多相关文章

  1. 【LeetCode】28. Implement strStr() (2 solutions)

    Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...

  2. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  3. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

  4. LeetCode(28)题解:Implement strStr()

    https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the fir ...

  5. 【LeetCode】28. Implement strStr() 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...

  6. 【LeetCode】28 - Implement strStr()

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

  7. LeetCode OJ:Implement strStr()(实现子字符串查找)

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

  8. 【LeetCode】028. Implement strStr()

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

  9. C# 写 LeetCode easy #28 Implement strStr()

    28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...

随机推荐

  1. [ASM C/C++] C语言数组

    固定长度的数组: 可以具有任何的存储类别. 长度可变的数组: 只能具有自动的生存周期(定义于语句块内,眀没有static修饰符). 名字必须为一般的标识符,因此结构或联合的成员不能是数组的标识符. 读 ...

  2. Discuz!模板解析语法

    <!--{eval echo autostart("); }--> PHP中使用template()函数显示已存在模板 在Discuz!程序执行中可以通过 include tem ...

  3. Linux删除多个java进程的其中一个

    一.背景: Linux后台运行了多个Java程序,进程名都是java. 执行pkill java会一次性杀掉所有的java进程. 二.解决思路: 先通过一定的检索条件,定位出指定的java进程 然后解 ...

  4. jq

    1: http://jquery.cuishifeng.cn/index.html jquery 学习查询首页<br> 2: http://m.oschina.net/blog/75741 ...

  5. div居中方法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. .Net环境下的缓存技术介绍

    .Net环境下的缓存技术介绍 摘要: 介绍缓存的基本概念和常用的缓存技术,给出了各种技术的实现机制的简单介绍和适用范围说明,以及设计缓存方案应该考虑的问题(共17页) 1         概念 1.1 ...

  7. C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)

    Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass() word对象 2. Microsoft.Offic ...

  8. alter system switch logfile与alter system archive log current的区别

    以前知道 ALTER SYSTEM SWITCH LOGFILE对单实例数据库或RAC中的当前实例执行日志切换, ALTER SYSTEM ARCHIVE LOG CURRENT会对数据库中的所有实例 ...

  9. ThinkPHP的URL重写+路由+伪静态,实现SEO效果。

    1.URL重写,隐藏网址中的Index.php. ThinkPHP 作为 PHP 框架,是单一入口的,那么其原始的 URL 便不是那么友好.但 ThinkPHP提供了各种机制来定制需要的 URL 格式 ...

  10. 【High-Speed and Accurate Laser Scan Matching Using Classified Features】

    所谓的"分类特征",就是把特征分成 1. 旋转特征:用直线表示 2. 平移特征,用撕裂点和临界点表示 最大的创新点 应该就是下面的分组吧 匹配的时候,用RANSAC 或者动态规划, ...