LeetCode 28:Implement strStr()

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

实现函数strStr。代码如下:

int strStr(char* haystack, char* needle) {
    size_t len = strlen(haystack);
    if(strlen(needle) == 0)
    return 0;
    if(strcmp(haystack, needle) == 0)
        return 0;
    for(int i=0; i<len;i++){
        if(haystack[i] == needle[0]){
            if(strncmp(&(haystack[i]), needle, strlen(needle)) == 0)
                return i;
        }
    }
    return -1;

}
时间: 2024-10-21 04:54:59

LeetCode 28:Implement strStr()的相关文章

LeetCode(28)题解:Implement strStr()

https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路: 判断一个string是否另一个string的子序列并返回位置. naive法:遍历查找,复杂度O(mn). advance法还有Rabi

leetcode笔记: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()函数.返回needle(关键字)在haystack(字符串)中第一次出现的位置,如果needle不在haystack中,则返回-1.由于使用暴力方法的时间复杂度为O(mn)会超时,可使用著名的KM

LeetCode(28)Implement strStr()

题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If you st

leetcode028:Implement strStr()

问题描述 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Update (2014-11-02): The signature of the function had been updated to return the index instead of the pointer. If you

Leetcode题目:Implement Queue using Stacks

题目:Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes:

LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))

Implement a trie with insert, search, and startsWith methods. 实现字典树,前面好像有道题做过类似的东西,代码如下: 1 class TrieNode { 2 public: 3 // Initialize your data structure here. 4 TrieNode():isLeaf(false) 5 { 6 for(auto & t : dic){ 7 t = NULL; 8 } 9 } 10 TrieNode * di

LeetCode 232: Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

LeetCode OJ:Implement Queue using Stacks(栈实现队列)

比较典型的一个题目,easy,不过可以有许多实现方式. 这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中.但是其他的实现也可以不是这样,可以是需要push的时候检查再,如果内容在stack2中,这时候将其倒回在进行push.这里采取第一种比较笨的方法,代码如下所示: 1 class Queue { 2 public: 3 // Push element x to the back of queue. 4 void push(int x) { 5 s1.push(x)

LeetCode 232:Implement Queue using Stacks

 Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: