TwoPointers-28-Implement strStr()
2018-02-05 14:58
211 查看
Description:
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Example 2:
Solution:
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll" Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba" Output: -1
Solution:
public class Solution { public int strStr(String haystack, String needle) { int l1 = haystack.length(), l2 = needle.length(); if (l1 < l2) { return -1; } else if (l2 == 0) { return 0; } int threshold = l1 - l2; for (int i = 0; i <= threshold; ++i) { if (haystack.substring(i,i + l2).equals(needle)) { return i; } } return -1; } }
相关文章推荐
- leetcode 第27-28 题 Implement strStr() & Divide Two Integers
- 18.leetcode题目28: Implement strStr()
- LeetCode 28 . Implement strStr()
- [LeetCode]28 Implement strStr()
- LeetCode 28 Implement strStr()(实现strStr()函数)
- LeetCode 28 — Implement strStr()(C++ Java Python)
- LeetCode 28 - Implement strStr()
- [Leetcode] #28 Implement strStr()
- LeetCode 28 Implement strStr()
- 28.Implement strStr()
- 28. Implement strStr()
- 28. Implement strStr()
- [28] Implement strStr()
- LeetCode#28 Implement strStr()
- Java [leetcode 28]Implement strStr()
- LeetCode 28 - Implement strStr()
- LeetCode 28: Implement strStr()
- Leetcode-28: Implement strStr()
- LeetCode 28 Implement strStr() (实现找子串函数)
- LeetCode-28-Implement strStr() KMP模板题