题目描述:

Implement strStr().

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

Update (2014-11-02):
The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char * or String, please click the reload button to reset your code definition.

解题思路:

从开头开始,逐个匹配。

注意特殊情况。

代码如下:

public class Solution {
public static int strStr(String haystack, String needle) {
int haystackLength = haystack.length();
int needleLength = needle.length();
int j = 1;
if (needleLength == 0)
return 0;
if (needleLength > haystackLength)
return -1;
for (int i = 0; i <= haystackLength - needleLength; i++) {
if (haystack.charAt(i) == needle.charAt(0)) {
for (j = 1; j < needleLength; j++) {
if (haystack.charAt(i + j) != needle.charAt(j))
break;
}
if (j == needleLength)
return i;
}
}
return -1;
}
}

Java [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(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. Leetcode #28. Implement strStr()

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

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

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

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

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

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

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

  8. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

  9. LeetCode——28. Implement strStr()

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

随机推荐

  1. PyQt4学习记录之事件和信号

    事件是任何 GUI程序中很重要的部分.所有 Python GUI 应用都是事件驱动的.一个应用对其生命期产生的不同的事件类型做出反应.事件是主要由应用的用户产生.但是,也可以通过其他方法产生,比如,网 ...

  2. ueditor使用中的坑

    项目中要使用富文本编辑于是采用了百度的开源富文本编辑器 ueditor    官网 http://ueditor.baidu.com/website/ 使用方法就按照官方的来的. 经过使用记录以下要点 ...

  3. 实时数据处理环境搭建flume+kafka+storm:2.flume 安装

    1.  解压  tar -zxvf     2.配置       拷贝配置文件 :cp flume-conf.properties.template flume-conf.properties     ...

  4. tar 解压缩命令

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  5. Codeforces Round #346 (Div. 2) E - New Reform 无相图求环

    题目链接: 题目 E. New Reform time limit per test 1 second memory limit per test 256 megabytes inputstandar ...

  6. 2013 Multi-University Training Contest 5 k-th point

    刚开始我也不知道怎么做,后来慢慢就推出来了…… 对于样例 2 1 0,结果是2/3 2 2 0,结果是4/5 3 2 0,结果是6/7 3 2 1,结果是9/14=6/7*3/4 …… 之后就会发现每 ...

  7. [Unity菜鸟] 笔记2 —— 问题篇

    记录在学习<Unity 3.x 游戏开发 经典教材>时遇到的各种问题与笔记 1. 初始不能降低Terrain的高度,需要到Terrain设置的第二个按钮中将Height从0调高 (注意:最 ...

  8. C++拷贝对象

    简介 对象的创建中,常常有这样的需求,就是把对象复制一份. 而复制有三种方法: 1.通过初始化来复制 例如:Object o1(10); Object o2=o1; 2.通过赋值来复制 例如:Obje ...

  9. node.js 模块之url和querystring模块

    关系如下: url.parse(string).query | url.parse(string).pathname | | | | | ------ ------------------- http ...

  10. (转)详解LVS负载均衡之三种工作模型原理和10种调度算法

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://linuxnx.blog.51cto.com/6676498/1195379 LV ...