LeetCode_28. Implement strStr()

28. Implement strStr()

Easy

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

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C‘s strstr() and Java‘s indexOf().

package leetcode;

public class ImplementStrStr {
	@org.junit.Test
	public void test() {
		String haystack1 = "hello";
		String needle1 = "ll";
		String haystack2 = "aaaaa";
		String needle2 = "bba";
		System.out.println(strStr(haystack1, needle1));
		System.out.println(strStr(haystack2, needle2));
	}

	public int strStr(String haystack, String needle) {
		char[] source = haystack.toCharArray();
		int sourceOffset = 0;
		int sourceCount = haystack.length();
		char[] target = needle.toCharArray();
		int targetOffset = 0;
		int targetCount = needle.length();
		int fromIndex = 0;
		if (fromIndex >= sourceCount) {
			return (targetCount == 0 ? sourceCount : -1);
		}
		if (fromIndex < 0) {
			fromIndex = 0;
		}
		if (targetCount == 0) {
			return fromIndex;
		}

		char first = target[targetOffset];
		int max = sourceOffset + (sourceCount - targetCount);

		for (int i = sourceOffset + fromIndex; i <= max; i++) {
			/* Look for first character. */
			if (source[i] != first) {
				while (++i <= max && source[i] != first)
					;
			}

			/* Found first character, now look at the rest of v2 */
			if (i <= max) {
				int j = i + 1;
				int end = j + targetCount - 1;
				for (int k = targetOffset + 1; j < end && source[j] == target[k]; j++, k++)
					;

				if (j == end) {
					/* Found whole string. */
					return i - sourceOffset;
				}
			}
		}
		return -1;
	}
}

原文地址:https://www.cnblogs.com/denggelin/p/11557870.html

时间: 2024-10-11 21:24:18

LeetCode_28. Implement strStr()的相关文章

leetcode_171 Excel Sheet Column Number &amp; leetcode_28 Implement strStr()

leetcode_171 Excel Sheet Column Number 题目: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. 解法: class Solution { public: int titleToNumber(string s) { int num = 0;

leetcode_28题——Implement strStr()(采用KMP算法,还没AC,但自己这边测试无误)

Implement strStr() Total Accepted: 49294 Total Submissions: 223057My Submissions Question Solution 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 s

LeetCode 28. Implement strStr()

https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 字符串简单题.一定要写的没BUG. 如果不在函数最后一行写return语句的话,LeetCode会出RUNTIME ERROR. Line 27: co

Implement strStr()

package cn.edu.xidian.sselab.string; /** *  * @author zhiyong wang * title: Implement strStr() * content: *  Implement strStr(). *   *  Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.  * */pub

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【leetcode】Implement strStr() (easy)

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路: 注意,在for循环中条件有计算得到的负数时, 一定要把计算括起来转换为int, 否则会默认转换为uchar 负数就会被误认为是一个很大的数字. for(int i = 0; i < int(1 - 2); ++i) 实现很常规: int s

LeetCode Implement strStr()

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

Implement strStr()——字符串中第一次出现给定字符串的位置

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41452047 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 func

28. Implement strStr()

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 非常容易的一道题目,注意各种特殊情况,先列举出corner case,再进行编码.学习参考经典实现. 这种关于字符串操作类的题目在面试时必须能够倒背如流. int strStr(char* haystack, char* needle) { if