找出字符串中第一个匹配项的下标

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

思路1、KMP算法

思路2、substring字串来替换下,一个一个检测。

class Solution {
public int strStr(String haystack, String needle) {
    class Solution {
public int strStr(String haystack, String needle) {
    for (int i = 0; i <= haystack.length() - needle.length(); i++) {
    // 先检查一下 haystack 剩下的长度是否足够提供 needle 的长度
    if (i + needle.length() <= haystack.length()) {
        String x = haystack.substring(i, i + needle.length());
        if (x.equals(needle)) {
            return i;
        }
    }
}
    return -1;
}
}
    return -1;在return 0;处,它应该返回-1而不是0。

如果 needle 为空字符串,则函数是合法的且返回 0。
但是如果 needle 不为空并且无法在 haystack 中找到,那么函数应该返回 -1 而不是 0,
来明确指出未找到对应的子字符串
}
}

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值