面试经典算法题46-找出字符串中第一个匹配项的下标

面试经典算法题46-找出字符串中第一个匹配项的下标

公众号:阿Q技术站

LeetCode.28

问题描述

给你两个字符串 haystackneedle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1

示例 1:

输入:haystack = "sadbutsad", needle = "sad"
输出:0
解释:"sad" 在下标 0 和 6 处匹配。
第一个匹配项的下标是 0 ,所以返回 0 。

示例 2:

输入:haystack = "leetcode", needle = "leeto"
输出:-1
解释:"leeto" 没有在 "leetcode" 中出现,所以返回 -1 。

思路

  1. 遍历 haystack 字符串,将当前字符与 needle 字符串的第一个字符进行比较。
  2. 如果找到了与 needle 第一个字符相等的字符,则从当前位置开始比较后续字符是否与 needle 字符串完全匹配。
  3. 如果匹配成功,则返回当前位置;否则,继续遍历 haystack 字符串。

图解

       +-------------------------+
       |       开始执行程序        |
       +-------------------------+
                   |
                   v
       +-------------------------+
       |       初始化变量          |
       |  haystack = "sadbutsad" |
       |  needle = "sad"         |
       |  needle_len = 3         |
       |  haystack_len = 9       |
       |  i = 0                  |
       +-------------------------+
                   |
                   v
       +-------------------------+
       |       进入匹配循环        |
       +-------------------------+
                   |
                   v
 +------------------------------------+
 |            检查是否匹配              |
 | haystack[i:i+needle_len] == needle |
 |      i = 0, "sad" == "sad", 匹配    |
 +------------------------------------+
                   |
                   v
      +-------------------------+
      |        输出结果          |
      |     第一个匹配项下标:0    |
      +-------------------------+

参考代码

C++
#include <iostream>
#include <string>

int strStr(std::string haystack, std::string needle) {
    int m = haystack.size(), n = needle.size();
    if (n == 0) return 0; // 如果 needle 是空字符串,返回 0
    for (int i = 0; i <= m - n; ++i) { // 从头到尾遍历 haystack
        if (haystack[i] == needle[0]) { // 找到与 needle 第一个字符相等的字符
            int j = 1;
            while (j < n && haystack[i + j] == needle[j]) ++j; // 比较后续字符是否匹配
            if (j == n) return i; // 完全匹配,返回当前位置
        }
    }
    return -1; // 未找到匹配项,返回 -1
}

int main() {
    std::string haystack = "hello", needle = "ll";
    int result = strStr(haystack, needle);
    std::cout << "匹配项的下标为:" << result << std::endl;
    return 0;
}
Java
#include <iostream>
#include <string>

int strStr(std::string haystack, std::string needle) {
    int m = haystack.size(), n = needle.size();
    if (n == 0) return 0; // 如果 needle 是空字符串,返回 0
    for (int i = 0; i <= m - n; ++i) { // 从头到尾遍历 haystack
        if (haystack[i] == needle[0]) { // 找到与 needle 第一个字符相等的字符
            int j = 1;
            while (j < n && haystack[i + j] == needle[j]) ++j; // 比较后续字符是否匹配
            if (j == n) return i; // 完全匹配,返回当前位置
        }
    }
    return -1; // 未找到匹配项,返回 -1
}

int main() {
    std::string haystack = "hello", needle = "ll";
    int result = strStr(haystack, needle);
    std::cout << "匹配项的下标为:" << result << std::endl;
    return 0;
}
Python
def strStr(haystack: str, needle: str) -> int:
    m, n = len(haystack), len(needle)
    if n == 0:
        return 0  # 如果 needle 是空字符串,返回 0
    for i in range(m - n + 1):  # 从头到尾遍历 haystack
        if haystack[i] == needle[0]:  # 找到与 needle 第一个字符相等的字符
            j = 1
            while j < n and haystack[i + j] == needle[j]:  # 比较后续字符是否匹配
                j += 1
            if j == n:  # 完全匹配,返回当前位置
                return i
    return -1  # 未找到匹配项,返回 -1

# 测试
haystack = "hello"
needle = "ll"
result = strStr(haystack, needle)
print("匹配项的下标为:", result)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值