您现在的位置是:首页 > 后台技术 > 数据结构与算法数据结构与算法

字符串暴力匹配(图文)

第十三双眼睛2023-10-22【数据结构与算法】人已围观

简介字符串暴力匹配

字符串暴力匹配
package com.xingchen.day016;
public class VoilenceMatch {
    public static void main(String[] args) {
        String s1 = "abcdefg";
        String s2 = "bvc";
        int i = voilenceMatch(s1, s2);
        System.out.println(i);
    }
    public static int voilenceMatch(String s1,String s2) {
        char[] chars1 = s1.toCharArray();
        char[] chars2 = s2.toCharArray();
        int s1len = chars1.length;
        int s2len = chars2.length;
        int i = 0;
        int j = 0;
        while (i<s1len && j< s2len) {
            if (chars1[i] == chars2[j]) {
                i ++;
                j ++;
            } else {
                i = i - (j - 1);
                j = 0;
            }
        }
        //判断是否匹配成功
        if (j == s2len) {
            return i - j;
        } else {
            return -1;
        }
    }
}

Tags:

很赞哦! ()

文章评论

    共有条评论来说两句吧...

    用户名:

    验证码:

本站推荐

站点信息

  • 网站名称:JavaStudy
  • 建站时间:2019-1-14
  • 网站程序:帝国CMS7.5
  • 文章统计242篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我们