leetcode c++ 简单题之实现 strStr()

leetcode c++ 简单题之实现 strStr()

题目要求

在这里插入图片描述

个人解答

class Solution {
public:
int strStr(string haystack, string needle) {
        int flag = 0;
        int i, j;
         int sizeofh = haystack.length();
         int sizeofn = needle.length();
        if (needle == "") {
            return 0;
        }
        for (i = 0; i < sizeofh; i++) {
            for (j = 0; j < sizeofn; j++) {
                if (i + j > sizeofh) {
                    break;
                }
                if (haystack[i + j] != needle[j]) {
                    break;
                }
            }
            if (j == sizeofn) {
                flag = 1;
                break;
            }
        }
        if (flag) {
            return i;
        }
        else return -1;
    }
};

在这里插入图片描述

官方答案

class Solution {
  public int strStr(String haystack, String needle) {
    int L = needle.length(), n = haystack.length();
    if (L == 0) return 0;

    int pn = 0;
    while (pn < n - L + 1) {
      // find the position of the first needle character
      // in the haystack string
      while (pn < n - L + 1 && haystack.charAt(pn) != needle.charAt(0)) ++pn;

      // compute the max match string
      int currLen = 0, pL = 0;
      while (pL < L && pn < n && haystack.charAt(pn) == needle.charAt(pL)) {
        ++pn;
        ++pL;
        ++currLen;
      }

      // if the whole needle string is found,
      // return its start position
      if (currLen == L) return pn - L;

      // otherwise, backtrack
      pn = pn - currLen + 1;
    }
    return -1;
  }
}

作者:LeetCode
链接:https://leetcode-cn.com/problems/implement-strstr/solution/shi-xian-strstr-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

个人总结

  • 这题几种思路1.kmp 2.双指针 3.调用api 虽然考研刚刚考过kmp 但我发现自己除了会手算next数组好像算法完全不会 又不想调用api(如java的indexOf()) 所以开始手搓双指针
  • 双指针感觉很简单 本来以为很快能解决 结果一直卡在超时这个问题上 之前除了死循环 似乎没遇到过这个问题 还是代码打的太少 于是乎开始研究怎么优化(下面贴上最早版本)
  • 首先把不断调用的string.size()函数早早定义成一个常量 肯定能大大减少时间开销 但很离谱的就是还是超时 卡了很久之后 突然意识到除了string.size()外还有string.length()可以用 抱着死马当活马医的态度试了一下 居然通过了 经验表明string.length()比string.size()的时间开销小
  • 做的时候 为了优化做尝试的时候顺便发现了 size_t 是 unsigned int 而不是 int 相应的 ssize_t 才是 int 总之学linux的对他们有印象 但显然还是搞混了
  • 欢迎大佬也能给我再提些建议
/*最早的版本*/
int strStr(string haystack, string needle) {
    int flag = 0;
    int i, j;
    if (needle == "") {
        return 0;
    }
    for (i = 0; i < haystack.size(); i++) {
        for (j = 0; j < needle.size(); j++) {
            if (i + j > haystack.size()) {
                break;
            }
            if (haystack[i + j] != needle[j]) {
                break;
            }
        }
        if (j == needle.size()) {
            flag = 1;
            break;
        }
      }
    if (flag == 1) {
        return i;
    }
    else return -1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值