Leecode刷题之旅-C语言/python-28.实现strstr()

/*
 * @lc app=leetcode.cn id=28 lang=c
 *
 * [28] 实现strStr()
 *
 * https://leetcode-cn.com/problems/implement-strstr/description/
 *
 * algorithms
 * Easy (37.86%)
 * Total Accepted:    38.6K
 * Total Submissions: 102K
 * Testcase Example:  ‘"hello"\n"ll"‘
 *
 * 实现 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() 定义相符。
 *
 */
int strStr(char* haystack, char* needle) {

    int i, j;

    int len1 = strlen(haystack);

    int len2 = strlen(needle);

    for(i = 0; i <= len1 - len2; i++){

        for(j = 0; j < len2; j++){

            if(haystack[i + j] != needle[j]){

                break;

            }

        }

        if(j == len2) return i;

    }

    return -1;

}

c语言自然是应用最最著名的kmp(看毛片)算法。

这个算法的理解可以参考:

https://www.cnblogs.com/yjiyjige/p/3263858.html

--------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=28 lang=python3
#
# [28] 实现strStr()
#
# https://leetcode-cn.com/problems/implement-strstr/description/
#
# algorithms
# Easy (37.86%)
# Total Accepted:    38.6K
# Total Submissions: 102K
# Testcase Example:  ‘"hello"\n"ll"‘
#
# 实现 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() 定义相符。
#
#
class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        l = len(needle)
        for i in range(len(haystack)-l+1):
            if haystack[i:i+l] == needle:
                return i
        return -1

python是真的简单,运用切片就能达到想要的目的了。

原文地址:https://www.cnblogs.com/lixiaoyao123/p/10493809.html

时间: 2024-08-30 06:47:57

Leecode刷题之旅-C语言/python-28.实现strstr()的相关文章

Leecode刷题之旅-C语言/python-26.删除数组中的重复项

/* * @lc app=leetcode.cn id=26 lang=c * * [26] 删除排序数组中的重复项 * * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/ * * algorithms * Easy (42.77%) * Total Accepted: 89.1K * Total Submissions: 208.1K * Testcase Example: '[

Leecode刷题之旅-C语言/python-26.移除元素

/* * @lc app=leetcode.cn id=27 lang=c * * [27] 移除元素 * * https://leetcode-cn.com/problems/remove-element/description/ * * algorithms * Easy (53.46%) * Total Accepted: 39.5K * Total Submissions: 73.7K * Testcase Example: '[3,2,2,3]\n3' * * 给定一个数组 nums 

Leecode刷题之旅-C语言/python-100相同的树

/* * @lc app=leetcode.cn id=100 lang=c * * [100] 相同的树 * * https://leetcode-cn.com/problems/same-tree/description/ * * algorithms * Easy (51.47%) * Total Accepted: 16K * Total Submissions: 31K * Testcase Example: '[1,2,3]\n[1,2,3]' * * 给定两个二叉树,编写一个函数来

Leecode刷题之旅-C语言/python-121买卖股票的最佳时机

/* * @lc app=leetcode.cn id=121 lang=c * * [121] 买卖股票的最佳时机 * * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/ * * algorithms * Easy (48.50%) * Total Accepted: 32.5K * Total Submissions: 66.9K * Testcase Example: '[7,1,5

Leecode刷题之旅-C语言/python-118杨辉三角

/* * @lc app=leetcode.cn id=118 lang=c * * [118] 杨辉三角 * * https://leetcode-cn.com/problems/pascals-triangle/description/ * * algorithms * Easy (60.22%) * Total Accepted: 17.6K * Total Submissions: 29.2K * Testcase Example: '5' * * 给定一个非负整数 numRows,生成

Leecode刷题之旅-C语言/python-141环形链表

/* * @lc app=leetcode.cn id=141 lang=c * * [141] 环形链表 * * https://leetcode-cn.com/problems/linked-list-cycle/description/ * * algorithms * Easy (35.53%) * Total Accepted: 28.4K * Total Submissions: 79.4K * Testcase Example: '[3,2,0,-4]\n1' * * 给定一个链表

Leecode刷题之旅-C语言/python-349两个数组的交集

/* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/intersection-of-two-arrays/description/ * * algorithms * Easy (60.49%) * Total Accepted: 15.1K * Total Submissions: 25K * Testcase Example: '[1,2,2,1]\n[2,2

Leecode刷题之旅-C语言/python-434 字符串中的单词数

/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/number-of-segments-in-a-string/description/ * * algorithms * Easy (29.13%) * Total Accepted: 4.2K * Total Submissions: 14.2K * Testcase Example: '"Hello, m

leecode刷题(26)-- 用栈实现队列

leecode刷题(26)-- 用栈实现队列 用栈实现队列 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是否为空. 示例: MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); queue.peek(); // 返回 1 queue.pop(); // 返回 1 queue.empty