【LeetCode從零單排】No28 Implement strStr()
題目
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
程式碼
public class Solution {
public int strStr(String haystack, String needle) {
if( needle.length()==0) return 0;
if(haystack.length()==0 ) return -1;
char[] haystack_char=haystack.toCharArray();
char[] needle_char=needle.toCharArray();
for(int i=0;i<haystack_char.length;i++){
a:
if(haystack_char[i]==needle_char[0]){
if(haystack_char.length-i>=needle_char.length){
for(int j=0;j<needle_char.length;j++){
if(haystack_char[i+j]!=needle_char[j]){
break a;
}
}
return i;
}
else{
return -1;
}
}
}
return -1;
}
}
/********************************
* 本文來自部落格 “李博Garvin“
* 轉載請標明出處:http://blog.csdn.net/buptgshengod
******************************************/
相關文章
- Leetcode Implement strStr()LeetCode
- Leetcode 28 Implement strStr()LeetCode
- Leetcode-Implement strStr()LeetCode
- Implement strStr() leetcode javaLeetCodeJava
- [LeetCode] Implement strStr() 實現strStr()函式LeetCode函式
- leetcode28_Implement strStr()LeetCode
- LeetCode-Implement strStr()-KMPLeetCodeKMP
- 【LeetCode 28_字串_匹配】Implement strStr()LeetCode字串
- 【leetcode】28. Implement strStr() 字串匹配KMP BMLeetCode字串匹配KMP
- 【LeetCode從零單排】No38.CountAndSayLeetCode
- 【LeetCode從零單排】No.7 Reverse IntegerLeetCode
- 【LeetCode從零單排】No20.ValidParenthesesLeetCode
- 【LeetCode從零單排】No19.RemoveNthNodeFromEndofListLeetCodeREM
- 【LeetCode從零單排】No21.MergeTwoSortedListsLeetCode
- 【LeetCode從零單排】No27.Remove ElementLeetCodeREM
- 【LeetCode從零單排】No22.Generate ParenthesesLeetCode
- 【LeetCode從零單排】No58.Length of Last WordLeetCodeAST
- 【LeetCode從零單排】No67.AddBinaryLeetCode
- 【LeetCode從零單排】No70.ClimbingStairsLeetCodeAI
- 【LeetCode從零單排】No.9 Palindrome NumberLeetCode
- 【LeetCode從零單排】No14.LongestCommonPrefixLeetCode
- 【LeetCode從零單排】No36 Valid SudokuLeetCode
- 【LeetCode從零單排】No221.Maximal SquareLeetCode
- 【LeetCode從零單排】No15 3SumLeetCode
- 【LeetCode從零單排】No189 .Rotate ArrayLeetCode
- 【LeetCode從零單排】No88.Merge Sorted ArrayLeetCode
- Mysql從零單排-1MySql
- 【LeetCode從零單排】No96 Unique Binary Search TreesLeetCode
- 【LeetCode從零單排】No112 Path SumLeetCode
- 【LeetCode從零單排】No.169 Majority Element(hashmap用法)LeetCodeHashMap
- 【LeetCode從零單排】No83 Remove Duplicates from Sorted ListLeetCodeREM
- 【LeetCode從零單排】No26.Remove Duplicates from Sorted ArrayLeetCodeREM
- 從零單排學Redis【白銀】Redis
- 【LeetCode從零單排】No 3 Longest Substring Without Repeating CharactersLeetCode
- 【LeetCode從零單排】No129 Sum Root to Leaf NumbersLeetCode
- 【LeetCode從零單排】No.160 Intersection of Two Linked ListsLeetCode
- 從零單排學Redis【黃金】Redis
- 【LeetCode從零單排】No121 Best Time to Buy and Sell StockLeetCode