第一题代码(0.82)
只过了0.82,有大佬可以帮忙看看吗
package 滴滴;
import java.math.BigInteger;
import java.util.Scanner;
public class Q1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
StringBuilder sb = new StringBuilder();
String ans[] = new String[1];
dfs(str,sb,0, ans);
System.out.println(ans[0]);
}
public static void dfs(String str, StringBuilder sb, int idx, String[] ans) {
if(idx>=str.length()) {
BigInteger bigInteger = new BigInteger(sb.toString());
if(bigInteger.mod(new BigInteger("3")).intValue()==0) {
ans[0] = sb.toString();
}
return;
}
if(str.charAt(idx)!='?') {
sb.append(str.charAt(idx));
dfs(str,sb,idx+1, ans);
sb.deleteCharAt(sb.length()-1);
}
else {
for (int i = 0; i < 10 && ans[0]==null; i++) {
if(i==0 && idx==0) continue;//不能有前导0
//判断和前一个是否相等
if(idx-1>=0 && (sb.charAt(idx-1)-'0')==i) {
continue;
}
//判断和后一个是否相等
if(idx+1<str.length() && (str.charAt(idx+1)!='?') && (str.charAt(idx+1)-'0')==i) {
continue;
}
sb.append((char)('0'+i));
dfs(str, sb, idx+1, ans);
sb.deleteCharAt(sb.length()-1);
}
}
}
}
第二题代码(ac)
package 滴滴;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeSet;
/*
5 2 1
1 1 3 6 10
12 3 5 12 15
1 2 1 2 1
*/
public class Q2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int p = sc.nextInt();
int q = sc.nextInt();
int l[] = new int[n];
int r[] = new int[n];
int t[] = new int[n];
TreeSet<Integer> set = new TreeSet<>();
for (int i = 0; i < n; i++) {
l[i] = sc.nextInt();
set.add(l[i]);
}
for (int i = 0; i < n; i++) {
r[i] = sc.nextInt();
set.add(r[i]);
set.add(r[i]+1);
}
for (int i = 0; i < n; i++) {
t[i] = sc.nextInt();
}
int idxCnt = set.size();
HashMap<Integer,Integer> idMap = new HashMap<>();
int[] reverseMap = new int[idxCnt+2];
int id = 1;
for (Integer idx : set) {
// System.out.println("map 映射:("+idx+"->"+id+")");
idMap.put(idx,id);
reverseMap[id++] = idx;
}
int diffP[] = new int[set.size()+2];
int diffQ[] = new int[set.size()+2];
int ans = 0;
for (int i = 0; i < n; i++) {
int idxL = idMap.get(l[i]);
int idxR = idMap.get(r[i]);
if(t[i]==1) {
diffP[idxL]++;
diffP[idxR+1]--;
}
else{
diffQ[idxL]++;
diffQ[idxR+1]--;
}
}/*
System.out.print("diffP:");
for (int i = 1; i < diffP.length; i++) {
System.out.print(diffP[i]+"\t");
}
System.out.print("\ndiffQ:");
for (int i = 1; i < diffQ.length; i++) {
System.out.print(diffQ[i]+"\t");
}
System.out.println();*/
for (int i = 1; i < diffP.length; i++) {
diffP[i] += diffP[i-1];
diffQ[i] += diffQ[i-1];
}
for (int i = 1; i < reverseMap.length-1; i++) {
// System.out.println("考虑当前id:"+i+"\tdiffP:"+diffP[i]+"\tdiffQ:"+diffQ[i]);
// System.out.println("\trealPos:"+reverseMap[i]+"\tnextPos:"+reverseMap[i+1]);
if(diffP[i]>=p && diffQ[i]>=q) {
ans+=(reverseMap[i+1]-reverseMap[i]);
}
}
/*
System.out.print("diffP:");
for (int i = 1; i < diffP.length; i++) {
System.out.print(diffP[i]+"\t");
}
System.out.print("\ndiffQ:");
for (int i = 1; i < diffQ.length; i++) {
System.out.print(diffQ[i]+"\t");
}
System.out.println();
System.out.println(diffP[set.size()]+"\t"+diffQ[set.size()]);*/
System.out.println(ans);
}
}
#23届秋招笔面经##2023一起秋招吧##滴滴笔试##滴滴##面经笔经#