• 28. 实现strStr()


    28. 实现strStr()

    https://leetcode-cn.com/problems/implement-strstr/description/

    竟然是KMP算法

    自己写的:http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html

    class Solution {
        public int strStr(String haystack, String needle) {
             if ("".equals(needle)) {
                return 0;
            }
            // 转换为数组
            char[] hayArray = haystack.toCharArray();
            char[] needArray = needle.toCharArray();
            // 从0开始进行判断
            for(int i=0;i<hayArray.length;i++) {
                // 如果第i个元素与needArray的第0个元素相等,就进行判断
                if (hayArray[i] - needArray[0] == 0) {
                    boolean isEqual = true;
                    // 判断数组中是否有不相等的元素
                    for(int j=0;j<needArray.length;j++) {
                        if (i + j >= hayArray.length) {
                            isEqual = false;
                            break;
                        }
                        if (hayArray[i + j] - needArray[j] != 0) {
                            isEqual=false;
                        }
                    }
                    // 如果都相等就返回i
                    if (isEqual) {
                        return i;
                    }
                }
            }
            // 如果都不相等就返回-1
            return -1;
        }
    }

    KMP算法:

    学习学习:http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html

    class Solution {
     public int strStr(String haystack, String needle) {
           
            if("".equals(needle)){
                return 0;
            }
            char[] t = haystack.toCharArray();
            char[] p = needle.toCharArray();
            int i = 0; // 主串的位置
            int j = 0; // 模式串的位置
            int[] next = getNext(needle);
            while (i < t.length && j < p.length) {
                if (j == -1 || t[i] == p[j]) { // 当j为-1时,要移动的是i,当然j也要归0
                    i++;
                    j++;
                } else {
                    // i不需要回溯了
                    // i = i - j + 1;
                    j = next[j]; // j回到指定位置
                }
            }
            if (j == p.length) {
                return i - j;
            } else {
                return -1;
            }
        }
    
        public static int[] getNext(String ps) {
            char[] p = ps.toCharArray();
            int[] next = new int[p.length];
            next[0] = -1;
            int j = 0;
            int k = -1;
            while (j < p.length - 1) {
                if (k == -1 || p[j] == p[k]) {
                    if (p[++j] == p[++k]) { // 当两个字符相等时要跳过
                        next[j] = next[k];
                    } else {
                        next[j] = k;
                    }
                } else {
                    k = next[k];
                }
            }
            return next;
        }
    }
  • 相关阅读:
    f5版本升级
    f5申请并激活License
    f5时间设置
    f5 SNMP配置
    f5 Syslog管理
    f5单台安装配置
    f5负载均衡算法
    f5 Seldom used
    f5售后查询
    f5基本介绍
  • 原文地址:https://www.cnblogs.com/stono/p/9495576.html
Copyright © 2020-2023  润新知