zoukankan      html  css  js  c++  java
  • Implement strStr()

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

    遍历算法

     1 public class Solution {
     2     public String strStr(String haystack, String needle) {
     3      
     4         if(haystack == null || needle == null) 
     5             return null;
     6         if(needle.length() == 0)
     7             return haystack;
     8         if(haystack.length() == 0)
     9             return null;
    10             
    11         int needleLen = needle.length();
    12         int haystackLen = haystack.length();
    13      
    14         for (int i = 0; i < haystackLen; i++) {
    15             // make sure in boundary of needle
    16             if (haystackLen - i + 1 < needleLen)
    17                 return null;
    18      
    19             int k = i;
    20             int j = 0;
    21      
    22             while (j < needleLen && k < haystackLen && needle.charAt(j) == haystack.charAt(k)) {
    23                 j++;
    24                 k++;
    25                 if (j == needleLen)
    26                     return haystack.substring(i);
    27             }
    28      
    29         }
    30      
    31         return null;
    32     }
    33 }

    KMP 算法

    public class Solution {
        public String strStr(String haystack, String needle) {
            if(haystack == null || needle == null) 
                return null;
            if(needle.length() == 0)
                return haystack;
            if(haystack.length() == 0)
                return null;
                
                char[] hay = haystack.toCharArray();
                char[] nee = needle.toCharArray();
                int Hlen = hay.length;
                int Nlen = nee.length;
                String str = match(hay, nee, Hlen, Nlen);
                return str;
        }
        
        public String match (char[] hay, char[] nee, int Hlen, int Nlen){
            int i = 0; int j=0;
            int[] next = getNext(nee, Nlen);
            while(i<Hlen) {
                if( j == -1 || hay[i] == nee[j]) {
                    i++;
                    j++;
                }else{
                    j =next[j];
                }
                if(j == Nlen){
                    String temp = String.valueOf(hay);
                    String result = temp.substring(i-Nlen);
                    return result;
                }
            }
            return null;
        }
        
        public int[] getNext(char[] nee, int Nlen){
            int[] next = new int[Nlen];
            int i =0; int k = -1;
            next[0] = -1;
            while(i< Nlen-1){
                if(k == -1 || nee[i] == nee[k]){
                    i++;
                    k++;
                    next[i] =k;
                }else{
                    k =next[k];
                }
            }
            return next;
        }
        
    }
  • 相关阅读:
    HTTP 无法注册 URL http://+:xxxxx/ServicesName/。进程不具有此命名空间的访问权限
    C语言中宏的一些特别用法
    static和const的比较和解释
    堆和栈的区别
    c++中const用法
    链表常见笔试题
    自绘实现半透明水晶按钮 .
    C++面试题
    C/C++面试题大汇总
    C++ 值传递、指针传递、引用传递详解
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3533420.html
Copyright © 2011-2022 走看看