Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

I wrote that function before in practice without knowing anything about strStr(), so I have a pretty clear brute-force solution in my mind.

 class Solution {
public:
char *strStr(char *haystack, char *needle) {
char *i = haystack;
char *j = needle;
int k = , m = ;
if (*j=='\0')return haystack;
while (*i != '\0'){
while (*(i+k)!='\0' && *(j+m)!=NULL && *(i+k) == *(j+m)){
k++;
m++;
}
if (k!= && m!= ){
if (*(j+m) == '\0'){
return i;
}else{
k=;m=;
}
}
i++;
}
return NULL;
}
};

Not very clean, with some unnecessary variable but express my thought well, unfortunately it's Time Limit Exceeded, means we can still do some optimize.
Some people says using KMP can definitely solve the problem, but its too hard to code(for me). I don't think people will ask to write KMP during an interview.
Then I find this thread the trick is, in fact we should aways only iterate N-M+1 times.

my AC version:

 class Solution {
public:
char *strStr(char *haystack, char *needle) {
char *i = haystack;
char *j = needle;
char *l = haystack;
int k = ;
if (!*j)return haystack; while (*++j){
l++;
}
j = needle; while (*l){
while (*(i+k) && *(j+k) && *(i+k) == *(j+k)){
k++;
}
if (k!=){
if (!*(j+k)){
return i;
}else{
k=;
}
}
i++;
l++;
}
return NULL;
}
};

[LeetCode] Implement strStr()的更多相关文章

  1. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

  3. LeetCode Implement strStr()(Sunday算法)

    LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...

  4. LeetCode: Implement strStr() [027]

    [题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...

  5. leetcode——Implement strStr() 实现字符串匹配函数(AC)

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  6. leetcode implement strStr python

    #kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: ...

  7. LeetCode Implement strStr() 实现strstr()

    如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...

  8. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  9. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

随机推荐

  1. Tip

    Windows 开栈命令 -Wl,--stack=1000000000 //stack-size B Linux 开栈命令 -ulimit -a -ulimit -s size //stack-siz ...

  2. 5.8---像素设定(CC150)

    注意:仔细注意位置. public static int[] renderPixel(int[] screen, int x, int y){ int[] ans = new int[screen.l ...

  3. 使用 MongoDB 的_id 查询

    MongoDB 默认在插入数据时,生成一个主键_id,那么怎么使用_id来查询数据? 查询全部 > db.foo.find(){ "_id" : ObjectId(" ...

  4. MySQL 编程的6个重要的技巧

    一.每一行命令都是用分号(;)作为结束 对于MySQL,第一件你必须牢记的是它的每一行命令都是用分号(;)作为结束的,但当一行MySQL被插入在PHP代码中时,最好把后面的分号省略掉,例如:   二. ...

  5. SQL Server中的索引

    1 SQL Server中的索引 索引是与表或视图关联的磁盘上结构,可以加快从表或视图中检索行的速度.索引包含由表或视图中的一列或多列生成的键.这些键存储在一个结构(B 树)中,使 SQL Serve ...

  6. AngularJS之Provider, Value, Constant, Service, Factory, Decorator的区别与详解

    本文转载自http://camnpr.com/javascript/1693.html 首先,provider, value, constant, service, factory他们都是provid ...

  7. 基于MATLAB的离散小波变换

    申明,本文非笔者原创,原文转载自:  基于Matlab的离散小波变换         http://blog.sina.com.cn/s/blog_725866260100ryh3.html 简介 在 ...

  8. linux skill

    linux console终端乱码解决 1.console终端乱码 在/etc/profile文件的最后一行添加如下内容: export LC_ALL="zh_CN.GB18030" ...

  9. (转)关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系

    转自http://www.cnblogs.com/cywin888/p/3263027.html 刚接触iOS开发的人难免会对苹果的各种证书.配置文件等不甚了解,可能你按照网上的教程一步一步的成功申请 ...

  10. Java for LeetCode 227 Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...