1.是否可以重排列为“Baidu”
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();//表示有多少组询问
scanner.nextLine();
String[] strs = new String[n];
for (int i = 0; i < n; i++) {
strs[i]=scanner.nextLine();
}
for (String str : strs) {
System.out.println(check(str));
}
}
public static String check(String s) {
if (s == null || s.length() == 0) return "No";
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
}
String r = "Baidu";
for (int i = 0; i < r.length(); i++) {
if (map.get(r.charAt(i)) == null) return "No";
else {
map.put(r.charAt(i), map.get(r.charAt(i)) - 1);
if (map.get(r.charAt(i)) == 0) map.remove(r.charAt(i));
}
}
return map.size() == 0 ? "Yes" : "No";
}
}
2.回文串
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long n = scanner.nextLong();
StringBuilder res=new StringBuilder();
long base=1;
while(base+base*base<=2*n){
base++;
}
base--;
for (int i = 0; i < base; i++) {
res.append("d");
}
long rest=n-(base*(1+base)/2);
for (int i = 0; i < rest; i++) {
if(i%3==0)res.append("r");
else if(i%3==1)res.append("e");
else res.append("d");
}
System.out.println(res);
}
}
3.重载方法
import java.util.HashSet;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();//待测数据
scanner.nextLine();
String[] strs = new String[n];
for (int i = 0; i < n; i++) {
strs[i] = scanner.nextLine();
}
Info[] infos = new Info[n];
int index = 0;
for (String str : strs) {
int i = str.indexOf(" ");
int left = str.indexOf("(");
int right = str.indexOf(")");
String backType = str.substring(0, i);
String funName = str.substring(i + 1, left);
String param = str.substring(left + 1, right);
String[] split = param.split(",");
String[] params = null;
if (!param.equals("")) {
params = new String[split.length];
for (int j = 0; j < split.length; j++) {
int spaceIndex = split[j].indexOf(" ");
params[j] = split[j].substring(0, spaceIndex);
}
}
infos[index++] = new Info(backType, funName, params);
}
HashSet<String> set = new HashSet<>();
for (int i = 0; i < n ; i++) {
String[] params = infos[i].params;
StringBuilder t=new StringBuilder();
if(params!=null){
for (String param : params) {
t.append(param);
}
}
boolean add = set.add(infos[i].funName + t);
if(add){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}
static class Info {
String backType;
String funName;
String[] params;
public Info(String backType, String funName, String[] params) {
this.backType = backType;
this.funName = funName;
this.params = params;
}
}
}
#百度笔试#