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. mysql 链接失败(ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES))

    mysql链接失败(ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)) 修改: # ...

  2. Android first---SQLite

    SQLite数据库 轻量级关系型数据库 创建数据库需要使用的api:SQLiteOpenHelper 必须定义一个构造方法: public MyOpenHelper(Context context, ...

  3. jquery 设置页面元素不可点击、不可编辑、只读(备忘)

    $("input").attr('readonly', true); $("textarea").attr('readonly', true); $(':rad ...

  4. crontab Job权限重要

    在撰写JOb时,需要将SH目录及文件设为777,方可正常执行~

  5. Fiddler 4 抓包

    Fiddler 4 Tools –> Fiddler Options HTTPS -> 勾选"CaptureHTTPS CONNECTs",同时勾选"Decr ...

  6. centos7 docker redis

    docker run --name=redistmp -ti centos /bin/bash yum -y install gcc tcl make cd /home wget http://dow ...

  7. MySQL 5.6 双机热备windows7

    MySQL 5.6 双机热备 目录: 1.说明 2.数据手工同步 3.修改主数据库配置文件 4.修改从数据库配置文件 5.主数据库添加备份用户 6.从数据库设置为Slave 7.验证 1.说明 1)数 ...

  8. Web开发中20个很有用的CSS库

    来源: 微信公众号文章 在过去的几年中,CSS已经成为一大部分开发者和设计者的最爱,因为它提供了一系列功能和特性.每个月都有无数个围绕CSS的工具被开发者发布以简化WEB开发.像CSS库,框架,应用这 ...

  9. Hadoop 集群搭建

    Hadoop 集群搭建 2016-09-24 杜亦舒 目标 在3台服务器上搭建 Hadoop2.7.3 集群,然后测试验证,要能够向 HDFS 上传文件,并成功运行 mapreduce 示例程序 搭建 ...

  10. jq弹出一个透明小提示窗,然后逐渐消失

      function show_main(content) { var showWindow = '<div id="show_main" style="borde ...