校招编程真题里边的实现通配符问题,一直显示数组越界,但是我检查了每个地方并没有发现数组越界情况,下边的是我的代码,请各位大佬帮忙指点一下
import java.util.Scanner; public class T174 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String TpStr = scanner.nextLine(); char PpStr[] = scanner.nextLine().toCharArray(); String[] newStr = TpStr.split("\\*"); char[] beforeStr = newStr[0].toCharArray(); char[] afterStr = newStr[1].toCharArray(); int startLocation = 0; int endLocation = 0; for (int i = 0;i<=PpStr.length-afterStr.length;i++){ boolean flagBool = true; if(beforeStr.length==0){ startLocation = i; }else { for (int j = 0;j<beforeStr.length;j++){ startLocation = i; if (beforeStr[j] != PpStr[j+i]){ flagBool = false; break; } } } if (!flagBool){ continue; } for (int m = beforeStr.length;m<=PpStr.length-afterStr.length;m++){ flagBool = true; if (afterStr.length==0){ endLocation = m; }else { for (int k = 0;k<afterStr.length;k++){ endLocation = m+k-startLocation+1; if (PpStr[m+k] != afterStr[k]) { flagBool = false; break; } } } if (flagBool){ if (startLocation>=0&&endLocation>0) System.out.println(startLocation+" "+endLocation); } } } } }
全部评论
(0) 回帖