字符串的朴素模式匹配算法

 1 package com.berry.algorithm.string;
 2 
 3 /**
 4  * Created by berry-h on 17-1-16.
 5  */
 6 public class NativeAlgorithm {
 7 
 8     /**
 9      * 字符串的朴素模式匹配算法
10      * @param mStr
11      * @param sStr
12      * @return
13      */
14     public Integer getStartIndex(String mStr, String sStr){
15         if(mStr == null || sStr ==null){
16             return null;
17         }
18 
19         char[] m = mStr.toCharArray();
20         char[] s = sStr.toCharArray();
21         int mLength = mStr.length();
22         int sLength = sStr.length();
23 
24         int i = 0;
25         int j = 0;
26 
27         while(i < mLength && j < sLength){
28 
29             if(m[i] == s[j]){
30                 i++;
31                 j++;
32             }else{
33                 i = i - j + 1;
34                 j = 0;
35             }
36 
37         }
38 
39         if(j >= sLength){
40             return i - sLength;
41         }else{
42             return null;
43         }
44     }
45 
46 }

 

转载于:https://www.cnblogs.com/kohakuhubo/p/6293643.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值