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

力扣:
实现 strStr() 函数。

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

输入输出:
示例 1:

输入: haystack = “hello”, needle = “ll”
输出: 2

示例 2:

输入: haystack = “aaaaa”, needle = “bba”
输出: -1

说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。首先有四种状态,haystack为空,needle不为空;haystack不为空,needle为空;haystack和needle都为空;或者二者都不为空。当needle为空时,不论haystack的长度为多少,直接返回0.

 class Solution 
    {
    public:
        int strStr(string haystack, string needle) 
        {
            int needle_len = needle.length();//获得两个字符串长度
            int hay_len = haystack.length();
            if(needle_len==0)
               return 0;            
            for (int i = 0; i <= hay_len - needle_len;)//从下标为0~hay_len-needle_len开始查到匹配字符
            {
                if (haystack[i] == needle[0] && haystack[i + needle_len - 1] == needle[needle_len - 1])//如果首尾字符都匹配,则比较中间,如果haystack长度比needle小,直接结束循环
                {
                    int first = 0; int flag = 0;
                    while (first<needle_len-1)
                    {
                        if (haystack[i + first] != needle[first])
                        {
                            flag = -1;
                            break;
                        }
                        else
                            first++;
                    }
                    if (flag == 0)//完全匹配
                        return i;
                }
                i++;
            }
            return -1;
        }
    };
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答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; } ``` 这个函数接收两个字符串参数haystackneedle返回needlehaystack第一次出现位置,如果没有匹配项,则返回-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字符串的第一个匹配项。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值