全部评论
我交的都是一个类里面一个处理方法,跟leetcode 形式一样,在自己的编译器上用主函数调试是对的,但是用页面上的都不行,这样可以吗
感觉。。。。。。。。。
感觉- -大家都AC了吧
import java.util.Comparator; import java.util.PriorityQueue; import java.util.Scanner; public class e1 { public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int count = 0;
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(15, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1;
}
}); int n = scanner.nextInt(); for (int i = 0; i < n; i++) { int num = scanner.nextInt(); if (count % 2 == 0) {
maxHeap.offer(num); int filteredMaxNum = maxHeap.poll();
minHeap.offer(filteredMaxNum);
} else {
minHeap.offer(num); int filteredMinNum = minHeap.poll();
maxHeap.offer(filteredMinNum);
}
count++;
} int m = scanner.nextInt(); for (int i = 0; i < m; i++) { int num = scanner.nextInt(); if (count % 2 == 0) {
maxHeap.offer(num); int filteredMaxNum = maxHeap.poll();
minHeap.offer(filteredMaxNum);
} else {
minHeap.offer(num); int filteredMinNum = minHeap.poll();
maxHeap.offer(filteredMinNum);
}
count++;
} if (count % 2 == 0) {
System.out.println(new Double((minHeap.peek() + maxHeap.peek())) / 2);
} else {
System.out.println(new Double((minHeap.peek())));
}
}
}
}
有携程two sums的编程题吗?
第二题明显用sort会超时吧,leetcode原题
import java.util.*; public class e { /*请完成下面这个函数,实现题目要求的功能 当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^ ******************************开始写代码******************************/ static int findMinMis(int[] A) { if (A == null) { return 0;
}
Arrays.sort(A); int num = -1; for (int i = 1; i < A.length; i++) { if (A[i] - A[i - 1] != 1) { if (A[i - 1] + 1 > 0) {
num = A[i - 1] + 1; break;
}
}
} if (num == -1) { if (A[0] > 1) {
num = A[0] - 1;
} else {
num = A[A.length - 1] + 1;
}
} return num;
} /******************************结束写代码******************************/ public static void main(String[] args) {
Scanner in = new Scanner(System.in); int res; int _A_size = 0;
_A_size = Integer.parseInt(in.nextLine().trim()); int[] _A = new int[_A_size]; int _A_item; for (int _A_i = 0; _A_i < _A_size; _A_i++) {
_A_item = Integer.parseInt(in.nextLine().trim());
_A[_A_i] = _A_item;
}
res = findMinMis(_A);
System.out.println(String.valueOf(res));
}
}
import java.util.LinkedHashSet; import java.util.Scanner; import java.util.Set; public class e3 { public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) {
String str = scanner.nextLine();
Set<Character> set = new LinkedHashSet<>(); for (int i = 0; i < str.length(); i++) {
Character c = str.charAt(i);
{ if (!set.contains(c)) {
System.out.print(c);
set.add(c);
}
}
}
System.out.println();
}
}
}
第二题我80什么情况
应该都是全AC吧。。
不全A都对不起自己呀
计算最长奔跑路径长度的题,有人做到吗,发个ac代码学习学习
相关推荐
投递民生银行等公司10个岗位 >
点赞 评论 收藏
分享