zoukankan      html  css  js  c++  java
  • [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.

    https://oj.leetcode.com/problems/implement-strstr/

    思路:目测有BF法,BMP法,BM法等。

    BF法:已删除。

    
    

    第二遍记录:

    BF法:

    public class Solution {
        public String strStr(String hayStack, String needle) {
            if (hayStack == null || needle == null)
                return null;
            char[] t = hayStack.toCharArray();
            char[] p = needle.toCharArray();
            int i=0,j=0;
            while(i<t.length&&j<p.length){
                if(t[i]==p[j]){
                    i++;
                    j++;
                }else{
                    i=i-j+1;
                    j=0;
                }
            }
            if(j==p.length){
                return hayStack.substring(i-j);
            }else{
                return null;
            }
            
            
        }
    }

    KMP法:

      注意空字符串“”,生成next数组时越界。next[0]=-1;

      注意相比BF方法,主函数需要判断j=-1的情况,因为next[j]可能等于-1而导致越界。

    public class Solution {
        public String strStr(String hayStack, String needle) {
            if (hayStack == null || needle == null)
                return null;
            char[] t = hayStack.toCharArray();
            char[] p = needle.toCharArray();
            int[] next = geneNext(needle);
            int i=0,j=0;
            while(i<t.length&&j<p.length){
                if(j==-1||t[i]==p[j]){
                    i++;
                    j++;
                }else{
                    j=next[j];
                }
            }
            if(j==p.length){
                return hayStack.substring(i-j);
            }else{
                return null;
            }
            
        }
        
        private int[] geneNext(String ps){
            char[] p = ps.toCharArray();
            int[] next = new int[p.length];
            if(next.length==0)
                return next;
            
            next[0]=-1;
            int j=0;
            int k=-1;
            
            while(j<p.length-1){
                if(k==-1||p[j]==p[k]){
                    j++;
                    k++;
                    next[j]=k;
                }else{
                    k=next[k];
                }
                
            }
            
            return next;
            
        }
        
        
    }

    第三遍记录:

      p为“”直接返回t。

      注意next数组的求法。

    第四遍记录:

    BF法都记不清了,给跪了,双指针法。i和j分别指向两个字符串的当前比较字符。

    public class Solution {
        public String strStr(String hayStack, String needle) {
            if(hayStack == null || needle == null)
                return null;
            // It will be convenient to change String to char array first
            char[] t = hayStack.toCharArray();
            char[] p = needle.toCharArray();
            
            int i =0,j=0;
            while(i<t.length&&j<p.length){
                if(t[i]==p[j]){
                    i++;
                    j++;
                }else{
                    //don't change j first because i will use j...
                    //i-j stands for the head of this comparison, +1 means to start from next char.
                    i = i-j+1;
                    j=0;
                }
            }
            if(j==p.length)
                return hayStack.substring(i-j);
            else
                return null;
            
        }
    }

    KMP

    基本思想:当失配时,i指针不回溯,只回溯j指针,j指针回溯到什么位置需要根据p字符串的结构计算出。

    public class Solution {
        public String strStr(String hayStack, String needle) {
            if(hayStack == null || needle == null)
                return null;
            //if needle is ""
            if(needle.isEmpty())
                return hayStack;
            // It will be convenient to change String to char array first
            char[] t = hayStack.toCharArray();
            char[] p = needle.toCharArray();
            
            int[] next = getNext(needle);
            
            int i =0,j=0;
            while(i<t.length&&j<p.length){
                //don't forget j == -1
                if(j==-1||t[i]==p[j]){
                    i++;
                    j++;
                }else{
                    j=next[j];
                }
            }
            if(j==p.length)
                return hayStack.substring(i-j);
            else
                return null;
            
        }
        
        private int[] getNext(String ps){
            // we assume ps is not ""
            char[] p = ps.toCharArray();
            int[] next = new int[p.length];
            
            next[0]=-1;
            int k =-1;
            int j = 0;
            //be careful p.length-1
            while(j<p.length-1){
                if(k==-1||p[k]==p[j]){
                    k++;
                    j++;
                    next[j] = k;
                }else{
                    k=next[k];
                }
                
            }
            
            return next;
        }
        
    }

    参考:

    http://www.cnblogs.com/yjiyjige/p/3263858.html

    http://blog.csdn.net/kenden23/article/details/17029625

    http://fisherlei.blogspot.com/2012/12/leetcode-implement-strstr.html

  • 相关阅读:
    组件GIS 0 前言
    GIS数据结构与算法
    GIS数据结构与算法 0 前言
    Git推送本地工程到远程仓库
    为知笔记+Typora+PicGo发表博客园博客
    时间记录"时间块"的使用技巧
    WebGIS学习路线
    [c++指针教程]用简单链表练习指针
    动态规划题目整理
    图论刷题整理
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3810718.html
Copyright © 2011-2022 走看看