前言
最近刷到一道很常见的算法题,题目不难,实现方式很多,就看时间复杂度哪种最低了。
话不多说,看题:
【问】 已知字符串str1=“ABABCDBADBCDBABCDBADBBADBBCCDBABCDBADBBCCDBADBBCCDBABCD”, str2=“ABCD”,如何快速高效的找出str2在str1中出现的所有下标索引以及第三次出现的下标索引?
博主一看:哇喔,这不是so easy 嘛??
开始操作。
1、indexof
public static void main(String[] args) {
String str1 = "ABABCDBADBCDBABCDBADBBADBBCCDBABCDBADBBCCDBADBBCCDBABCD";
String str2 = "ABCD";
System.out.println(getIndexs(str1, str2));
System.out.println(getIndex(str1, str2));
}
/**
* 每找到一个“ABCD”,就将字符串截取,并将每次截取位置+下标累加即可
*
* @param str1
* @param str2
* @return
*/
static List<Integer> getIndexs(String str1, String str2) {
int length = str2.length();
int index;
int location = 0;
List<Integer> indexs = new ArrayList<>();
while (-1 != str1.indexOf(str2)) {
index = str1.indexOf(str2);
indexs.add(location + index);
index = index + length;
location = location + index;
str1 = str1.substring(index);
}
return indexs;
}
/**
* 每找到一个“ABCD”,就将字符串截取,直到找到第三个,将每次下标累加即可。
*
* @param str1
* @param str2
* @return
*/
static Integer getIndex(String str1, String str2) {
int num = 0;
int location = 0;
int index = str1.indexOf(str2);
while (-1 != str1.indexOf(str2)) {
location += index;
num++;
if (3 == num) {
break;
} else {
str1 = str1.substring(index);
index = str1.indexOf(str2, 1);
}
}
return location;
}
2、正则
public static void main(String[] args) {
String str1 = "ABABCDBADBCDBABCDBADBBADBBCCDBABCDBADBBCCDBADBBCCDBABCD";
String str2 = "ABCD";
System.out.println(getIndexs(str1, str2));
System.out.println(getIndex(str1, str2));
}
static List<Integer> getIndexs(String str1, String str2) {
List<Integer> indexs = new ArrayList<>();
int index;
Pattern p = Pattern.compile(str2);
Matcher m = p.matcher(str1);
while (m.find()) {
index=m.start();
indexs.add(index);
}
return indexs;
}
static Integer getIndex(String str1, String str2) {
int index = 0;
int num = 0;
Pattern p = Pattern.compile(str2);
Matcher m = p.matcher(str1);
while (m.find()) {
num++;
if (3 == num) {
index=m.start();
}
}
return index;
}
3、split
public static void main(String[] args) {
String str1 = "ABABCDBADBCDBABCDBADBBADBBCCDBABCDBADBBCCDBADBBCCDBABCD";
String str2 = "ABCD";
System.out.println(getIndexs(str1, str2));
System.out.println(getIndex(str1, str2));
}
static List<Integer> getIndexs(String str1, String str2) {
int length = str2.length();
String[] split = str1.split(str2);
int index = 0;
List<Integer> indexs = new ArrayList<>();
for (String s : split) {
indexs.add(index + s.length());
index = index + s.length() + length;
}
return indexs;
}
static Integer getIndex(String str1, String str2) {
int length = str2.length();
String[] split = str1.split(str2);
int index = 0;
int num = 0;
for (String s : split) {
num++;
index = index + s.length();
if (num == 3) {
break;
}
index = index + length;
}
return index;
}
4、计算hash
public static void main(String[] args) {
String str1 = "ABABCDBADBCDBABCDBADBBADBBCCDBABCDBADBBCCDBADBBCCDBABCD";
String str2 = "ABCD";
System.out.println(getIndexs(str1, str2));
System.out.println(getIndex(str1, str2));
}
static List<Integer> getIndexs(String str1, String str2) {
int length = str2.length();
int length1 = str1.length();
int i = str2.hashCode();
List<Integer> indexs = new ArrayList<>();
for (int i2 = 0; i2 < length1 - length + 1; i2++) {
String substring = str1.substring(i2, i2 + length);
int i1 = substring.hashCode();
if (i == i1) {
indexs.add(i2);
}
}
return indexs;
}
static Integer getIndex(String str1, String str2) {
int length = str2.length();
int length1 = str1.length();
int i = str2.hashCode();
int index = 0;
int num = 0;
for (int i2 = 0; i2 < length1 - length + 1; i2++) {
String substring = str1.substring(i2, i2 + length);
int i1 = substring.hashCode();
if (i == i1) {
num++;
if (num == 3) {
index = i2;
break;
}
}
}
return index;
}
这几种方式的运行结果:
完结!