<每日 1 OJ> -LeetCode 28. 实现 strStr()

题目:

实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。

示例 1:

输入: haystack = "hello", needle = "ll"
输出: 2
示例 2:

输入: haystack = "aaaaa", needle = "bba"
输出: -1
说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

思路:

此处是C的实现,需要用到3个指针

/**

输入: haystack = "hello", needle = "ll"
1.使用双指针,指针i指向haystack的开始位置,指针j指向needle的开始位置
2.初始i=0,j=0,t=0。haystack[i]=h,needle[l]=l。以haystack串作为外循环
3.第一次循环判断,haystack[i]&&needle[j]有值满足条件,进入第一次循环
做判断haystack[i]=h等于needle[l]=l不成立,t=t+1=1,i=1,j=0
4.第二次循环判断,haystack[i]=e,needle[l]=l,haystack[i]&&needle[j]有值满足条件,进入第二次循环
做判断haystack[i]=e等于needle[l]=l不成立,t=t+1=2,i=2,j=0
5.第三次循环判断,haystack[i]=l,needle[l]=l,haystack[i]&&needle[j]有值满足条件,进入第三次循环
做判断haystack[i]=l等于needle[l]=l成立,i=3,j=1,continue接下来的代码不走,直接进入新的循环判断
6.第四次循环判断,haystack[i]=l,needle[l]=l,haystack[i]&&needle[j]有值满足条件,进入第四次循环
做判断haystack[i]=l等于needle[l]=l成立,i=4,j=2,continue接下来的代码不走,进入第五次循环
7.第五次循环判断,haystack[i]=e,needle[l]=‘\0‘,haystack[i]&&needle[j]不满足条件,跳出循环
8.判断needle[j] == ‘\0‘满足条件,返回t=2,结束

代码:

// 28. 实现 strStr().cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "stdlib.h"

int strStr(char * haystack, char * needle){
    int i = 0,j =0,t=i;//一定要有t
    while (haystack[i]&&needle[j]){
        if (haystack[i] == needle[j]){
            i++;
            j++;
            continue;
            //C 语言中的 continue 语句有点像 break 语句。但它不是强制终止,continue 会跳过当前循环中的代码,强迫开始下一次循环。
        } else{
            t=t+1;
            i=t;
            j=0;
        }
    }
    //如果needle串到了结尾的时候,代表顺利找到
    if (needle[j] == ‘\0‘){
        return t;
    }
    //否则返回-1,没找到
    return -1;
}

int _tmain(int argc, _TCHAR* argv[])
{

    int strStr(char * haystack, char * needle);
    char *str1 = (char*)malloc(100);
    char *str2 = (char*)malloc(100);
    // char *str = NULL;  错误的用法
    scanf("%s %s",str1,str2);
    int result=strStr(str1, str2);
    printf("%d",result);
    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/mhq-martin/p/11380612.html

时间: 2024-08-02 06:35:37

<每日 1 OJ> -LeetCode 28. 实现 strStr()的相关文章

前端与算法 leetcode 28.实现 strStr()

# 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和indexOf一样快),但我们还是要去了解api内实现的原理,在我们所熟悉的v8引擎中,indexOf使用了kmp和bm两种算法,在主串长度小于7时使用kmp,大于7的时候使用bm,bf咱就不说了哈,那个其实就是爆破算法, 提示 数据结构,kmp,bm 解析 kmp算法的核心其实就是动态规划,明确了

44. leetcode 28. Implement strStr()

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. 思路:子串匹配,朴素匹配.

leetCode 28. Implement strStr() 字符串

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. 在haystack中找与needle 第一个相匹配的位置.如果找不到,返回-1. 代码如下: class Solution { public:     int strStr(string haystac

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

19.1.30 [LeetCode 28] Implement strStr()

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",

[LeetCode] 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. 解法: 这道题让我们在一个字符串中找另一个字符串第一次出现的位置,那我们首先要做一些判断,如果子字符串为空,则返回0,如果子字符串长度大于母字符串长度,则返回-1.然后我们开始遍历母字符串,我们并不需要遍历整个母字符串,而是遍历到剩下的长度和子字符

Java [leetcode 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. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If you

LeetCode 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. 翻译: 找到字符串的子串位置,并返回.如果没有则返回-1 思路: 通过最简单的BF遍历,如果不符合则指向下一个字符,最后如果长度等于子串长度,则返回位置. 代码: public static int strStr(String haysta

LeetCode 28 Implement strStr() (C,C++,Java,Python)

Problem: 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