题目描述:

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. (转)MVC 3 数据验证 Model Validation 详解

    继续我们前面所说的知识点进行下一个知识点的分析,这一次我们来说明一下数据验证.其实这是个很容易理解并掌握的地方,但是这会浪费大家狠多的时间,所以我来总结整理一下,节约一下大家宝贵的时间. 在MVC 3 ...

  2. 长安CS15_手动——16款

    一.输入数据 1.CAN总线描述:位置,颜色,速率,总线类型 1)位置:OBD 2)颜色:3) 速率:500k 4)总线类型:HSCAN 5)测试时间:2016.5.4 2.车辆特征 1)排量:1.5 ...

  3. DEVICE_ATTR的使用

    DEVICE_ATTR的使用 使用DEVICE_ATTR,可以在sys fs中添加“文件”,通过修改该文件内容,可以实现在运行过程中动态控制device的目的. 类似的还有DRIVER_ATTR,BU ...

  4. 备份了一个nginx的虚拟主机配置文件报错

    [root@localhost vhost]# service nginx restart 停止 nginx:[确定] 正在启动 nginx:nginx: [warn] conflicting ser ...

  5. 快捷设置IE代理小工具

    时间:2015-02-06 起因: 公司新装了PLM系统,用这个系统必须使用指定IP段的IP才能访问.所以为了还能愉快的继续使用代理进行特定网站的访问,我们必须要频繁的去设置IE代理,这也太麻烦了吧. ...

  6. .net sql connection pool leak

    Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This ma ...

  7. java 取小数点后两位 不四舍五入,怎么做

    java 取小数点后两位 不四舍五入,怎么做 正常版: //正常版: import java.text.DecimalFormat; import java.math.RoundingMode; De ...

  8. uva 10534

    一开始WA了  参考了一下   求正反两个方向的最长上升子序列  并分别记录在两个数组中  最后求最大值 #include <iostream> #include <cstdio&g ...

  9. Spark 1.60的executor schedule

    第一次看源码还是Spark 1.02.这次看新源码发现调度方式有了一些新的特征,在这里随便写一下. 不变的是,master还是接收Appclient和worker的消息,并且在接收RegisterAp ...

  10. http://www.linuxidc.com/Linux/2007-09/7399.htm

    http://www.linuxidc.com/Linux/2007-09/7399.htm