您的位置:首页 > 其它

28. Implement strStr()

2016-06-27 09:55 288 查看
题目:https://leetcode.com/problems/implement-strstr/

代码:

public c
b0f1
lass Solution {
public int strStr(String haystack, String needle) {
if(needle.length()>haystack.length())
return -1;
return haystack.indexOf(needle);
}
}
用String的自带函数
1ms
=======================
public class Solution {
public int strStr(String haystack, String needle) {
if(needle.length()>haystack.length())
return -1;
int lengthh = haystack.length();
int lengthn = needle.length();
int index=0,i,j;
while(lengthh>=lengthn)
{
i=0;
j = index;
int flag = 0;
while(i<lengthn)
{
if(haystack.charAt(j)!=needle.charAt(i))
{
flag = 1;
break;
}
i++;j++;
}
if(flag == 0)
return index;
else
{
index++;lengthh--;
}
}
return -1;
}
}
4ms
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: