Leetcode题解(九)

28、Implement strStr()-------KMP算法(*)

题目

这道题目其实就是实现KMP算法,并且该算法也是比较经典的算法,需要很好的掌握:

贴上几个介绍字符串匹配的算法说明链接

http://www.cnblogs.com/Su-30MKK/archive/2012/09/17/2688122.html

本题实现代码:

 1 class Solution {
 2 public:
 3     int strStr(string haystack, string needle) {
 4          if("" == needle)
 5             return 0;
 6         if("" == haystack )
 7             return -1;
 8
 9         return kmp(haystack,needle);
10
11     }
12     int kmp(const std::string& s, const std::string& p, const int sIndex = 0)
13     {
14         std::vector<int>next(p.size());
15         getNext(p, next);//获取next数组,保存到vector中
16
17         int i = sIndex, j = 0;
18         while(i != s.length() && j != p.length())
19         {
20             if (j == -1 || s[i] == p[j])
21             {
22                 ++i;
23                 ++j;
24             }
25             else
26             {
27                 j = next[j];
28             }
29         }
30
31         return j == p.length() ? i - j: -1;
32     }
33     void getNext(const std::string &p, std::vector<int> &next)
34     {
35         next.resize(p.size());
36         next[0] = -1;
37
38         int i = 0, j = -1;
39
40         while (i != p.size() - 1)
41         {
42             //这里注意,i==0的时候实际上求的是next[1]的值,以此类推
43             if (j == -1 || p[i] == p[j])
44             {
45                 ++i;
46                 ++j;
47                 next[i] = j;
48             }
49             else
50             {
51                 j = next[j];
52             }
53         }
54     }
55 };
时间: 2024-10-20 16:56:01

Leetcode题解(九)的相关文章

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/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

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

LeetCode题解

Reverse Words in a String 考虑几个特殊的情况1.若字符窜s="  "2.字符窜s=“a  b  d     e”3.字符窜s=“ a” class Solution { public: void reverseWords(string &s) { int i; int cas=0; string st[100]; s+=' '; for(i=0;i<s.size();i++) { if(i==0 && s[0]==' ') con

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

[LeetCode 题解]: Reverse Nodes in K-Groups

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

[LeetCode 题解]: Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 题意: 给定一个链表,找到环起始的位置.如果环不存在,返回NULL. 分析: (1)首先要判断该链表是否有环.如果没有环,那么返回NULL. (2)其次,当已知环存在后,寻找环起始的位置. 思路: (

[LeetCode 题解]:Candy

There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies

[LeetCode: 题解] Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) will