招商银行信用卡中心 第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);
    }
}




#笔试题目##秋招##招商银行信用卡中心##Java#
全部评论
似乎发错版块了,应该发在我要提问
点赞 回复
分享
发布于 2019-09-15 22:52
也是这样,后来想了想会不会是因为权重较大,需要使用long类型才可以
点赞 回复
分享
发布于 2019-09-15 22:53
百信银行
校招火热招聘中
官网直投

相关推荐

点赞 12 评论
分享
牛客网
牛客企业服务