选择题,桶排序是否稳定,以太网的交换机能否较好地实现***,C++11的新特性,O(1)空间复杂度是否能找出数组中出现一半的数。
简单题,不使用事务和锁,如何实现多线程并发访问数据库。
代码题是牛客网上常见的题。
一点资讯
1题 活动安排。计算可参加的最大活动数
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param timeSchedule string字符串ArrayList<ArrayList<>> * @return int整型 */ public int countMaxActivity (ArrayList<ArrayList<String>> timeSchedule) { if (timeSchedule == null || timeSchedule.size() == 0) return 0; timeSchedule.sort((o1, o2)->{ return o1.get(0).equals(o2.get(0)) ? o1.get(1).compareTo(o2.get(1)): o1.get(0).compareTo(o2.get(0)); }); int count = 1; String ed = timeSchedule.get(0).get(1); for (int i = 1; i < timeSchedule.size(); ++i) { ArrayList<String> event = timeSchedule.get(i); if (ed.compareTo(event.get(0)) <= 0) { count++; ed = event.get(1); } } return count; } }2题最小路径
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mapArray int整型二维数组 * @return int整型 */ public int findMin (int[][] mapArray) { if (mapArray.length == 0 || mapArray[0].length == 0) return 0; for (int i = 1; i < mapArray.length; ++i) { mapArray[i][0] += mapArray[i-1][0]; } for (int i = 1; i < mapArray[0].length; ++i) { mapArray[0][i] += mapArray[0][i-1]; } for (int i = 1; i < mapArray.length; ++i) { for (int j = 1; j < mapArray[0].length; ++j) { mapArray[i][j] += Math.min(mapArray[i-1][j], mapArray[i][j-1]); } } return mapArray[mapArray.length-1][mapArray[0].length-1]; } }
3题版本号比较
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param version_list string字符串二维数组 输入参数为版本号列表,其中每个元素包含两个版本号 * @return int整型一维数组 */ public int[] max_version(String[][] version_list) { int[] ret = new int[version_list.length]; for (int i = 0; i < version_list.length; ++i) { ret[i] = compare(version_list[i][0], version_list[i][1]); } return ret; } private int compare(String a, String b) { int i = 0, j = 0; while (i < a.length() && j < b.length()) { int num1 = 0, num2 = 0; while (i < a.length() && a.charAt(i) != '.') { num1 = num1 * 10 + a.charAt(i++) - '0'; } ++i; while (j < b.length() && b.charAt(j) != '.') { num2 = num2 * 10 + b.charAt(j++) - '0'; } ++j; if (num1 < num2) return 2; if (num1 > num2) return 1; } while (j < b.length()) { int num2 = 0; while (j < b.length() && b.charAt(j) != '.') { num2 = num2 * 10 + b.charAt(j++) - '0'; } ++j; if (num2 != 0) { return 2; } } return 1; } }
1题 两坐标点之间的距离
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long R = sc.nextInt(); long[][] arr = new long[4][2]; for (int i= 0; i < 4; ++i) { arr[i][0] = sc.nextInt(); arr[i][1] = sc.nextInt(); } int cnt = 0; for (int i = 1; i <= 3; ++i) { cnt += (arr[i][0] - arr[0][0]) * (arr[i][0] - arr[0][0]) + (arr[i][1] - arr[0][1]) * + (arr[i][1] - arr[0][1]) <= R * R ? 1: 0; } System.out.println(cnt); } }2题 两个堆实现在线查询中位数
import java.util.Collections; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Queue<Double> maxQue = new PriorityQueue<>(Collections.reverseOrder()); Queue<Double> minQue = new PriorityQueue<>(); int n = sc.nextInt(); StringBuilder cache = new StringBuilder(); while(n-- > 0) { double num = sc.nextDouble(); cache.append(String.format("%.2f", getMidNumber(maxQue, minQue, num))).append("\n"); } System.out.println(cache); } private static double getMidNumber(Queue<Double> maxQue, Queue<Double> minQue, double num) { if (minQue.size() == maxQue.size()) { if (minQue.isEmpty()) { minQue.add(num); return num; } else { if (minQue.peek() <= num) { minQue.add(num); } else { maxQue.add(num); minQue.add(maxQue.poll()); } return minQue.peek(); } } else { if (minQue.peek() < num) { minQue.add(num); maxQue.add(minQue.poll()); } else { maxQue.add(num); } return maxQue.peek(); } } }
全部评论
(6) 回帖