3.12美团笔试解法
1
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int i = 0; i < n; i++) { int num = scanner.nextInt(); if (isNum(num)) { System.out.println("yes"); }else { System.out.println("no"); } } } private static boolean isNum(int num) { if (num % 11 == 0) { return true; } int count = 0; while (num != 0) { if (num % 10 == 1) { count ++; } num /= 10; } return count >= 2; } }
2
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = scanner.nextInt(); } int[] preSum = getPreSum(nums); int count = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // [i, j] if ((preSum[j + 1] - preSum[i]) % 2 == 0) { count ++; } } } System.out.println(count); } private static int[] getPreSum(int[] nums) { // res[i] [0,i-1]之间的-1的个数 int[] res = new int[nums.length + 1]; for (int i = 0; i < nums.length; i++) { res[i + 1] = res[i]; if (nums[i] == -1) { res[i + 1] ++; } } return res; } }
3
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); int[][] nums = new int[n][2]; for (int i = 0; i < n; i++) { nums[i][0] = scanner.nextInt(); nums[i][1] = scanner.nextInt(); } boolean[] visited = new boolean[m + 1]; Arrays.fill(visited, false); int res = fun(nums, n - 1, visited); System.out.println(res); } /** * 下一个尝试满足的顾客下表 [0,n-1] */ public static int fun(int[][] nums, int i, boolean[] used) { if (i < 0) { return 0; } // 不满足当前顾客 int res = fun(nums, i - 1, used); // 满足当前顾客 int[] customer = nums[i]; if (!used[customer[0]] && !used[customer[1]]) { used[customer[0]] = true; used[customer[1]] = true; res = Math.max(res, fun(nums, i - 1, used) + 1); used[customer[0]] = false; used[customer[1]] = false; } return res; } }
4
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); int[] nums = new int[m]; for (int i = 0; i < nums.length; i++) { nums[i] = scanner.nextInt(); } int res = fun(nums, n); System.out.println(res); } private static int fun(int[] times, int n) { Map<Integer, List<Integer>> map = new HashMap<>(); for (int i = 1; i <= n; i++) { map.put(i, new ArrayList<>()); } for (int i = 1; i < times.length; i++) { List<Integer> list = map.get(times[i]); list.add(i); } int curPos = 1; int count = 0; for (int i = 0; i + 1 < times.length; i++) { if (times[i + 1] == curPos) { // 当前所在节点下一秒爆炸,找到下一个最晚爆炸的点 int maxIndex = -1; int maxValue = 0; for (int j = 1; j <= n; j++) { List<Integer> list = map.get(j); if (list.isEmpty()) { maxIndex = j; maxValue = Integer.MAX_VALUE; break; } int k = 0; for (; k < list.size() && list.get(k) <= i; k++) { } if (k == list.size()) { maxIndex = j; maxValue = Integer.MAX_VALUE; }else { Integer time = list.get(k); if (time > maxValue) { maxIndex = j; maxValue = time; } } } // 删除所有maxValue之前的时间 curPos = maxIndex; count ++; // // 移除<=maxValue的所有时间 // for (int j = 1; j <= n; j++) { // List<Integer> list = map.get(j); // Iterator<Integer> iterator = list.iterator(); // while (iterator.hasNext()) { // if (iterator.next() <= maxValue) { // iterator.remove(); // } // } // } } } return count; } }
5
import javax.swing.tree.TreeNode; import java.util.*; public class Main { public static class TreeNode { int val; List<TreeNode> children = new ArrayList<>(); public TreeNode(int val) { this.val = val; } } private static int white = 0; private static int black = 0; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int nodes = scanner.nextInt(); int[] colors = new int[nodes]; int[] parents = new int[nodes]; for (int i = 0; i < colors.length; i++) { colors[i] = scanner.nextInt(); } for (int i = 0; i < parents.length; i++) { parents[i] = scanner.nextInt(); } TreeNode root = toTree(parents); white = 0; black = 0; dfs(root, colors); System.out.println(white + " " + black); } /** * root不为空 */ private static void dfs(TreeNode root, int[] colors) { int color = colors[root.val - 1]; if (color == 0) { // 白色 int count = 0; if (root.children.isEmpty()) { count = 1; }else { for (TreeNode child : root.children) { if (colors[child.val - 1] == 1) { count = 1; break; } } } white += count; }else { // 黑色 int count = 0; if (root.children.isEmpty()) { count = 1; }else { count = 1; for (TreeNode child : root.children) { if (colors[child.val - 1] != 0) { count = 0; break; } } } black += count; } for (TreeNode child : root.children) { dfs(child, colors); } } public static TreeNode toTree(int[] parents) { Map<Integer, TreeNode> map =new HashMap<>(); TreeNode root = new TreeNode(-1); for (int i = 0; i < parents.length; i++) { if (parents[i] == 0) { root.val = i + 1; map.put(i + 1, root); } } for (int i = 0; i < parents.length; i++) { int child = i + 1; int parent = parents[i]; TreeNode parentNode = map.computeIfAbsent(parent, v -> new TreeNode(v)); TreeNode childNode = map.computeIfAbsent(child, v -> new TreeNode(v)); parentNode.children.add(childNode); } return root; } }