招商银行信用卡中心 第1题AC,第二题自己觉得对,但通过率0
第一题代码
import java.util.Scanner; /* RRRRRLRLRL */ public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); int n = line.length(); if (line.length() < 2 || line.charAt(0) != 'R' || line.charAt(n-1) != 'L'){ throw new IllegalArgumentException("input error"); } int[] result = new int[n]; scanner.close(); for (int i = 0; i < n; i++){ result[i] = 1; } if (line.charAt(1) == 'R'){ result[2] += result[0]; result[0] = 0; } if (line.charAt(n-2) == 'L'){ result[n-3] += result[n-1]; result[n-1] = 0; } for (int i = 1; i < n-1; i++){ if (line.charAt(i) == 'R' && line.charAt(i+1) == 'R'){ result[i+2] += result[i]; result[i] = 0; } } for (int i = n-2; i > 0; i--){ if (line.charAt(i) == 'L' && line.charAt(i-1) == 'L'){ result[i-2] += result[i]; result[i] = 0; } } for (int i = 0; i < n-1; i++){ System.out.print(result[i] + " "); } System.out.println(result[n-1]); } }第二题代码,第二题自己随便写几个测试用例都可以通过,但提交通过率为0
import java.util.*; public class Main { public static void main(String[] args){ Map<Integer, Integer> map = new HashMap<>(); Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int maxNumber = 1; int[][] matrix = new int[n-1][3]; for (int i = 0; i < n-1; i++){ for (int j = 0; j < 3; j++){ matrix[i][j] = scanner.nextInt(); if (j < 2){ maxNumber = Math.max(maxNumber, matrix[i][j]); } } } scanner.close(); Arrays.sort(matrix, (Comparator.comparingInt(o -> o[0]))); for (int i = n-2; i >= 0; i--){ Integer weight = map.computeIfAbsent(matrix[i][0], k->0); int tempWeight; tempWeight = Math.max(weight, map.computeIfAbsent(matrix[i][1], k->0) + matrix[i][2]); map.put(matrix[i][0], Math.max(tempWeight, matrix[i][2])); } for (int i = 1; i < n; i++){ Integer weight = map.computeIfAbsent(i, k -> 0); System.out.print(weight + " "); } Integer weight = map.computeIfAbsent(n, k -> 0); System.out.println(weight); } }