代码随想录算法训练营day9 | 28. 实现 strStr() //找出字符串中第一个匹配项的下标

28. 实现 strStr() (medium)

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        if needle == "" or haystack == "":
            return 0
        
        base = 131
        n = len(haystack)
        h = [0] * (n+1)
        p = [0] * (n+1)
        p[0] = 1
        
        for i in range(1,n+1):
            h[i] = h[i-1] * base + ord(haystack[i-1])
            p[i] = p[i-1] * base
            
        hash = 0 
        for i in range(len(needle)):
            hash = hash * base + ord(needle[i])
            
        for i in range(1, n-len(needle)+1+1):
            Hash = h[i+len(needle)-1] - h[i-1] * p[len(needle)]
            
            if Hash == hash:
                return i-1
            
        return -1
  • 个人理解(不一定对,后续会继续更新...):

    • KMP & 字符串哈希_是饿梦啊的博客-CSDN博客
    • 字符串哈希实际上就是将一个字符串映射成一个数字,这样就可以通过这个数字来定位唯一字符串,且每次询问都是O(1)的复杂度。
      • Remark:这里的哈希(hash)并不是密码学里的hash函数比如sha256(), 由于之前研究过密码学的一点内容,所以第一反应就跟这个联系起来,但是思路是一样的,利用同一字符串唯一哈希值来定位。因此这里本意是转化成数字,那么在python中可以用 ord() 来实现
    • base是一个经验值,是一个质数,减少碰撞的可能性。
    • 如果出现碰撞,那么就进行两次哈希,大概率没问题。
    • 没有继续使用取模操作,是因为(from 芒果冰)
      • “Java如果超过最大值会自己取模的, c++好像是需要手动取模一下,python也有自己的大数处理“,所以不需要进行取模。
    • 待补...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以的,以下是Java代码实现: ``` public int strStr(String haystack, String needle) { if (needle.isEmpty()) { return 0; } int hLen = haystack.length(); int nLen = needle.length(); if (hLen < nLen) { return -1; } for (int i = 0; i <= hLen - nLen; i++) { if (haystack.substring(i, i + nLen).equals(needle)) { return i; } } return -1; } ``` 这个函数接收两个字符串参数haystack和needle,返回needle在haystack第一次现的位置,如果没有匹配项,则返回-1。函数使用substring()函数来比较haystack的每个子字符串是否与needle相等,从而实现字符串匹配。 ### 回答2: 可以使用Java字符串方法来实现题目要求。以下是一种可能的解决方案: ```java public class Solution { public int strStr(String haystack, String needle) { if (needle.length() == 0) { return 0; } for (int i = 0; i <= haystack.length() - needle.length(); i++) { if (haystack.substring(i, i + needle.length()).equals(needle)) { return i; } } return -1; } } ``` 以上代码,我们首先判断 needle 是否为空字符串,如果是,则直接返回 0,因为空字符串一定是 haystack 的子串。然后,我们使用 for 循环遍历从 0 到 haystack.length() - needle.length() 的下标范围。在循环过程,我们使用 haystack.substring(i, i + needle.length()) 获取长度与 needle 相等的子串,并将其与 needle 进行比较。如果相等,则返回当前下标 i,表示到了子串的第一个匹配项。如果循环结束后仍未匹配项,则说明 needle 不是 haystack 的子串,返回 -1。 以上就是根据题目要求用 Java 代码编写的解决方案。 ### 回答3: 可以使用Java代码来实现在haystack字符串needle字符串的第一个匹配项的下标,代码如下所示: ```java public class FindIndex { public static int findIndex(String haystack, String needle) { if (haystack == null || needle == null || needle.length() > haystack.length()) { return -1; } int haystackLen = haystack.length(); int needleLen = needle.length(); for (int i = 0; i <= haystackLen - needleLen; i++) { int j; for (j = 0; j < needleLen; j++) { if (haystack.charAt(i + j) != needle.charAt(j)) { break; } } if (j == needleLen) { return i; } } return -1; } public static void main(String[] args) { String haystack = "hello world"; String needle = "world"; int index = findIndex(haystack, needle); System.out.println("在haystack字符串到needle字符串的第一个匹配项的下标为:" + index); } } ``` 以上代码通过双重循环遍历haystack字符串和needle字符串,逐个字符进行比较,如果到相同的字符序列,则返回对应的下标位置。如果遍历完整个haystack字符串仍未匹配项,则返回-1。在上述代码,给定的haystack字符串为"hello world",needle字符串为"world",其输结果为: ``` 在haystack字符串到needle字符串的第一个匹配项的下标为:6 ``` 表示在haystack字符串下标为6处到了needle字符串的第一个匹配项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值