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.easy;

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;
}
}

LeetCode_28. Implement strStr()的更多相关文章

  1. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. 28. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  5. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  6. 【leetcode】Implement strStr() (easy)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. [LeetCode] Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  8. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. Implement strStr() [LeetCode]

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

随机推荐

  1. cookie和session基础知识学习

    一.session的简单使用 session是服务器端技术,服务器在运行时可以为每一个用户的浏览器创建一个独享的session对象.session的使用步骤: 获取session对象使用session ...

  2. 常考JS题笔记

    ### 1. 原始类型有哪几种?null 是对象吗? 答: Null,undefined,Number,String,Blooean,symbol1)[理解和使用ES6中的Symbol][https: ...

  3. 多线程下,使用new实现单例

    import threading class Test(object): from threading import Lock lock = Lock() flag = None def __new_ ...

  4. 智能灯控(基于ZigBee)

    时间:2017年12月 阶段:大二上学期 背景:单片机原理与应用课设 名称:智能灯控 摘要 本系统实现了多方式控灯功能,有按键控灯.串口指令控灯.点对点无线射频控灯.AI模式控灯.其中AI模式控灯是通 ...

  5. PHP实现多文件上传的一些简单方法

    下面我们就通过具体的代码示例,为大家介绍PHP实现多文件上传的一些简单方法. 第一种方法:利用单个文件上传方法 一段简单的form表单代码如下: <!DOCTYPE html> <h ...

  6. 二维bit模板

    #include<bits/stdc++.h> using namespace std; typedef long long ll; #define N 1100 const int mo ...

  7. 【概率论】5-3:超几何分布(The Hypergeomtric Distribution)

    title: [概率论]5-3:超几何分布(The Hypergeomtric Distribution) categories: - Mathematic - Probability keyword ...

  8. mac 启动mysql

    sudo /usr/local/mysql/support-files/mysql.server stop sudo /usr/local/mysql/support-files/mysql.serv ...

  9. WPF中打开网页的两种方法

    1.浏览器打开 Process proc = new System.Diagnostics.Process(); proc.StartInfo.FileName = "http://www. ...

  10. docker 搭建registry

    Docke官方提供了Docker Hub网站来作为一个公开的集中仓库.然而,本地访问Docker Hub速度往往很慢,并且很多时候我们需要一个本地的私有仓库只供网内使用.Docker仓库实际上提供两方 ...