跳至主要內容

leetcode-28 实现strStr()

postleetcode小于 1 分钟

leetcode-28 实现strStr()

题目描述:

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

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/implement-strstr 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

我的第一版答案:

class Solution {
    public int strStr(String haystack, String needle) {
        if (haystack == null) return 0;
        if (needle == null) return 0;
        int haystackLength = haystack.length();
        int needleLength = needle.length();
        if (needleLength > haystackLength) {
            return -1;
        }
        
        for (int i = 0; i < (haystack.length() - needleLength + 1); i++) {
            int j =0;
            while(j < needleLength) {
                if (haystack.charAt(i + j) == needle.charAt(j)) {
                    j ++;
                } else {
                    break;
                }
            }
            if (j == needleLength) {
                return i;
            }
        }
        return -1;
    }
}

版权申明

本站点所有内容,版权均归https://wenchao.renopen in new window所有,除非明确授权,否则禁止一切形式的转载协议

打赏

微信 支付宝

上次编辑于:
打赏
给作者赏一杯咖啡吧
您的支持将是我继续更新下去的动力
微信微信
支付宝支付宝