【LeetCode】Implement strStr()

Implement strStr()

Implement strStr().

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

标准KMP算法。可参考下文。

http://blog.csdn.net/yaochunnian/article/details/7059486

核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在next数组中。


class Solution
{
public:
int getlen(char *str)
{
int i;
for(i = 0; str[i] != ‘\0‘; i ++)
;
return i;
}
void getnext(char *str, int len, int next[])
{
int i = 0;
next[i] = -1;
int j = -1;

while(i < len-1)
{
if(j==-1 || str[i]==str[j])
{
i++;
j++;
if(str[i] == str[j])
next[i] = next[j];
else
next[i] = j;
}
else
j = next[j];
}
}

char *strStr(char *haystack, char *needle)
{
int hlen = getlen(haystack);
//cout << "hlen:" << hlen << endl;
int nlen = getlen(needle);
//cout << "nlen:" << nlen << endl;

int *next = new int[nlen];
getnext(needle, nlen, next);

int i = 0;
int j = 0;
while(i != hlen && j != nlen)
{
if(j == -1 || haystack[i] == needle[j])
{
i++;
j++;
}
else
{
j = next[j];
}
}
if(j == nlen)
return &haystack[i-nlen];
else
return NULL;
}
};

【LeetCode】Implement strStr(),布布扣,bubuko.com

时间: 2024-09-28 18:28:03

【LeetCode】Implement strStr()的相关文章

【leetcode】Implement strStr() (easy)

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路: 注意,在for循环中条件有计算得到的负数时, 一定要把计算括起来转换为int, 否则会默认转换为uchar 负数就会被误认为是一个很大的数字. for(int i = 0; i < int(1 - 2); ++i) 实现很常规: int s

【LeetCode】Implement Stack using Queues 解题报告

[题目] Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empty() -- Return whether the stack is empty. Notes: You must use o

【LeetCode】双指针 two_pointers(共47题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [19]Remove Nth Node From End of List [26]Remove Duplicates from Sorted

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【LeetCode】Pow(x, n)

Implement pow(x, n). 思路:快速幂运算,需要考虑指数为负数,同时底数为0的情况,这种属于异常数据,代码里没有体现. class Solution { public: double pow_abs(double x, unsigned int n) { if (n == 0) { return 1; } if (n == 1) { return x; } double res = pow(x, n>>1); res *= res; if (n & 0x1 == 1)

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【LeetCode】LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. set

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

【LeetCode】Next Permutation 解题报告

[题目] Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The repl