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. Shape + Selector: Make a Shape as one item of the Selector

    Generally, I use a selector to select pictures or colors to render the normal and the pressed backgr ...

  2. 关于linux中文乱码的问题。

    公司让人在一台装有ubuntu14.04的机器上安装net-snmp,可是这台机器的设置很让人不喜.没关系,一个个解决它. 不能连接外网,得弄一个代理. 这个好说,在可以上外网的本机上安装squid工 ...

  3. 数据统计表插件,highcharts插件的简单应用

    highcharts插件的简单应用,非常全能好用的一个数据统计表插件. $(function () { $('#container').highcharts({ chart:{ type:" ...

  4. MFC----任务管理器的制作

    首先建立一个MFC项目,因为进程有多个并且是动态的,所以可以看做链表,获得头结点&&依次向下遍历: 首先 我们使用CreateToolhelp32Snapshot提取出进程表,之后

  5. NOIP 2010题解

    唔..NOIP2010比较简单,总体感觉不错. Problem 1: 机器翻译 水题,队列的简单应用. 读入时判断是否在内存中,可以用hash优化.如果不在内存中push进内存,放不下了pop hea ...

  6. 【IDEA】IDEA 如何设置编辑器字体大小

    intellij idea 如何更改编辑器文本字体和大小   换上了intellij idea之后,第一件事就是想要改变下文字字体,因为在我这个27寸的2k分辨率的屏幕上,文字显然太小了. intel ...

  7. SQL Server数据库主键与索引的几点区别

    我们在使用SQL Server数据库的时候常常会创建主键和索引,那么主键和索引到底有什么样的不同呢?本文我们主要介绍了主键和索引的区别. 主键和索引的区别如下: 主键是索引,但索引不一定是主键. 主键 ...

  8. iOS 在UITableViewCell中加入自定义view时view的frame设定注意

    由于需要重用同一个布局,于是在cellForRowAtIndexPath中把自定义view加在了cell上,我是这样设定view的frame的 var screenFrame = UIScreen.m ...

  9. 安装CentOS 7时出现No Caching mode page found问题的解决

    将CentOS 7镜像刻到U盘之后,向服务器安装时,使用U盘启动会出现两种启动选项,一种是UEFI启动选项,一种是默认的启动选项,如果不使用UEFI方式安装,那么一般是没有问题的,如果选择UEFI方式 ...

  10. Single Number II

    题目: Given an array of integers, every element appears three times except for one. Find that single o ...