zoukankan      html  css  js  c++  java
  • 字符串总结,主要是kmp与双指针

    字符串总结

    字符串常用的算法思想,双指针翻转
    kmp:通过记录前缀避免每次重新查找时在从头找

     1import java.util.ArrayList;
    2import java.util.List;
    3
    4/**
    5 * 28. 实现 strStr()
    6 * 实现 strStr() 函数。
    7 * <p>
    8 * 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1。
    9 * <p>
    10 * 示例 1:
    11 * <p>
    12 * 输入: haystack = "hello", needle = "ll"
    13 * 输出: 2
    14 * 示例 2:
    15 * <p>
    16 * 输入: haystack = "aaaaa", needle = "bba"
    17 * 输出: -1
    18 * 说明:
    19 * <p>
    20 * 当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
    21 * <p>
    22 * 对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
    23 */

    24public class StrStr {
    25
    26    /**
    27     * kmp算法
    28     * 构建next前缀数组,存储匹配个数的数组
    29     * 步骤
    30     * 初始化
    31     * 处理前缀不相同情况
    32     * 处理前缀相同情况
    33     * 注意的是在处理前后缀不相同时 j要想后走,如果是第一位就不走了
    34     * 处理前后缀相同的时候j先加一,然后把 j的值 赋给next[i]
    35     *
    36     * @param haystack
    37     * @param needle
    38     * @return
    39     */

    40    private static int[] getNext(int[] next, String needle) {
    41        int j = 0;
    42        next[0] = j;
    43        for (int i = 1; i < needle.length(); i++) {
    44            while (j > 0 && needle.charAt(j) != needle.charAt(i)) {
    45                j = next[j - 1];
    46            }
    47
    48            if (needle.charAt(j) == needle.charAt(i)) {
    49                j++;
    50            }
    51            next[i] = j;
    52        }
    53        return next;
    54    }
    55
    56    /**
    57     * 这里是直接用next 不进行任何操作
    58     * @param haystack 文本串
    59     * @param needle   模式串
    60     * @return
    61     */

    62    public static int strStr(String haystack, String needle) {
    63        if ("".equals(needle)) {
    64            return 0;
    65        }
    66
    67        int[] next = new int[needle.length()];
    68        next = getNext(next, needle);
    69
    70        int j = 0;
    71        for (int i = 0; i < haystack.length(); i++) {
    72            while (j > 0 && haystack.charAt(i) != needle.charAt(j)) {
    73                j = next[j - 1];
    74            }
    75            if (haystack.charAt(i) == needle.charAt(j)) {
    76                j++;
    77            }
    78            if (j == needle.length()) {
    79                return i - needle.length() + 1;
    80            }
    81        }
    82        return -1;
    83    }
    84
    85    public static void main(String[] args) {
    86        String str1 = "abaaba";
    87        String str2 = "abaf";
    88        System.out.println(strStr(str1, str2));
    89    }
    90}
    不会,我可以学;落后,我可以追赶;跌倒,我可以站起来!
  • 相关阅读:
    js正则表达式大全(2)
    Magic Trackpad 2 on win10 x64
    Google 日历短信通知没有了
    Ueditor 1.4.3 jsp utf-8版Bug修复
    [转]eclipse中build workspace的相关优化
    Hello,
    EpCloud开发日志
    为服务创建安装程序
    winform 通过WCF上传Dataset数据
    opcrcw.da.dll 和.net 4.0
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/14467377.html
Copyright © 2011-2022 走看看