您的位置:首页 > 编程语言 > Java开发

【Leetcode】:28. Implement strStr()问题 in JAVA

2016-06-22 20:32 666 查看
Implement strStr().

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

Subscribe to see which companies asked this question
题目要求求字符串needle第一次在haystack中出现的位置
------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Solution {
public int strStr(String haystack, String needle) {
if (needle.length() == 0) {
return 0;
}
if (haystack.length() == 0) {
return -1;
}
int result = -1;
point:
for (int index = 0; index <= haystack.length() - needle.length(); index++){
for(int i = 0; i < needle.length(); i++) {
if (haystack.charAt(index + i) != needle.charAt(i)){
break;
}
if (i == needle.length() - 1) {
result = index;
break point;
}
}
}
return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java leetcode string