algorithm-关于leetcode上的Implement Strstr()的一个疑问

问题描述

关于leetcode上的Implement Strstr()的一个疑问

问题 : https://oj.leetcode.com/problems/implement-strstr/

我的解答:

 int strStr(char *haystack, char *needle) {
        if (!*needle) return 0;
        if (!*haystack) return -1;
        char* ph, *pn;
        ph = haystack;
        for (int i = 0;*ph; ++i, ++ph)
        {
            char* py = ph;
            pn = needle;
            while (*py && *pn && *py == *pn)
            {
                ++py;
                ++pn;
            }
            if (!*pn)
                return i;
        }
        return -1;
 }

虽然知道代码的效率有待改进,比如haystack末尾长度少于needle的不用检测,用KMP等,但是就上面的代码,我觉得虽然效率低,但是应该可以通过,结果提示Time Limit Exceeded, 我就想是不是我的代码真的有问题,但是在本地运行好像得到的结果还对呢,这是我的问题,还是online judge的问题呢

解决方案

改进的方法可以成功,我是想知道这个问题出在哪里

解决方案二:

不知道规则是否允许,你在函数中直接调用标准库中的strstr可以么?如果可以,那么如果那个还超时就是oj的问题,否则就是你算法的问题。

解决方案三:

谢谢楼上各位,发现问题了,是当haystack末尾长度少于needle时,提前退出就好了

  if (!*pn)
                return i;

```//在此之后加上这个检测就Ok 了
if (!*py)
                        return -1;

解决方案四:

谢谢楼上各位,发现问题了,是当haystack末尾长度少于needle时,提前退出就好了

  if (!*pn)
                return i;

```//在此之后加上这个检测就Ok 了
if (!*py)
                        return -1;

解决方案五:

直接用库自带的strstr提交看一下

解决方案六:

既然你支持了改进的地方,也可以尝试修改一下

时间: 2024-09-17 04:00:23

algorithm-关于leetcode上的Implement Strstr()的一个疑问的相关文章

LeetCode 28 Implement strStr()(实现strStr()函数)

翻译 实现strStr()函数. 返回针(needle)在草垛/针垛(haystack)上第一次出现的索引, 如果不存在其中则返回-1. 其实也就是说字符串str2在字符串str1中第一次出现的索引而已. 原文 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 代码 class Solution

[LeetCode]28.Implement strStr()

[题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. [题意] 实现strStr()功能. [分析] 暴力算法代码如下.更高效的的算法有 KMP 算法.Boyer-Mooer 算法和Rabin-Karp 算法.面试中暴力算法足够了. 具体参考:KMP字符串模式匹配详解 [代码] /***

【LeetCode从零单排】No28 Implement strStr()

题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 代码 public class Solution { public int strStr(String haystack, String needle) { if( needle.length()==0) return 0; if(hayst

编程-leetcode上的一题,Reverse Bits?

问题描述 leetcode上的一题,Reverse Bits? 1C 题目如下:Reverse bits of a given 32 bits unsigned integer.For example given input 43261596 (represented in binary as 00000010100101000001111010011100) return 964176192 (represented in binary as00111001011110000010100101

在leetcode上提交zigzag后,一直爆出RE错误,感觉应该没有越界?

问题描述 在leetcode上提交zigzag后,一直爆出RE错误,感觉应该没有越界? RT,原题在这:https://oj.leetcode.com/problems/zigzag-conversion/ 很简单的一个程序,就是横排字符串改成折线字符串,然后横排输出 我的程序在VS2012上 debug模式下自己测试没找到错误,但是提交上去之后, 就报Runtime-error,百思不得其解,求大神指教解救! char *convert(char *s, int nRows) { long l

leetcode上我customtest是对的,提交确是错的,题目本身很简单

问题描述 leetcode上我customtest是对的,提交确是错的,题目本身很简单 https://leetcode.com/discuss/68338/dont-know-whats-wrong-with-code-does-well-ide-and-customtest 代码点进去就有,

Implement strStr()

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 这个题应该就是求子串的问题,改进的方法是kmp算法. 朴素算法的C++代码如下: #include<iostream> #include<cstring> using namespace std; class Solution { p

LeetCode 31 Next Permutation(下一个排列)

翻译 实现"下一个排列"函数,将排列中的数字重新排列成字典序中的下一个更大的排列. 如果这样的重新排列是不可能的,它必须重新排列为可能的最低顺序(即升序排序). 重排必须在原地,不分配额外的内存. 以下是一些示例,左侧是输入,右侧是输出: 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 原文 Implement next permutation, which rearranges numbers into the lexicographically

bitmap-在Bitmap或者OVERLAYING按钮上如何动态的创建一个按钮

问题描述 在Bitmap或者OVERLAYING按钮上如何动态的创建一个按钮 我想在视图的 BITMAP或者OVERLAYING按钮上面创建一个按钮.我创建了一个数字签名,签名后,我需要点击一个按钮来保存.我使用的如下代码 public class FingerPaint extends GraphicsActivity implements OnClickListener { private TextView pauseButton;protected void onCreate(Bundle