返回信息流他写了50多分钟还没写出来,给他挂了[em21]
这是一条镜像帖。来源:北邮人论坛 / iwhisper / #6769811同步于 2024/1/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
IWhisper机器人发帖
作为面试官,出这个题对于字节人难吗
IWhisper#734
2024/1/23镜像同步27 回复
订阅后,新回复会通过你的通知中心匿名送达。
27 条回复
第一次面试od,自己出的题。正则中?代表0个或1个,现可以保证用户输入某参数仅会出现0次或1次,例如对于两个参数时,可以用正则a?b?a?可以表示a, b, ab, ba,和空5种传参情况。 a?b?c?a?b?c?a?可以表示三个参数时候的全部传参情况。 请问n个参数1,2.....n。用最少?且满足所有传参情况的正则表达,结果用list<>表示。
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
int n = 3; // 设置参数个数
List<String> regexList = generateRegex(n);
System.out.println(regexList);
}
private static List<String> generateRegex(int n) {
List<String> regexList = new ArrayList<>();
// 生成初始的正则表达式
StringBuilder regex = new StringBuilder();
for (int i = 1; i <= n; i++) {
regex.append("a?");
}
// 递归生成所有传参情况
generateAllCombinations(regexList, regex.toString(), 0, n);
return regexList;
}
private static void generateAllCombinations(List<String> regexList, String regex, int index, int n) {
if (index == n) {
regexList.add(regex);
return;
}
// 复制当前的正则表达式
StringBuilder newRegex = new StringBuilder(regex);
// 在当前位置增加一个参数
newRegex.insert(index * 2 + 1, "b?");
generateAllCombinations(regexList, newRegex.toString(), index + 1, n);
// 在当前位置不增加参数
generateAllCombinations(regexList, regex, index + 1, n);
}
}