华为OD机试真题 - 篮球游戏

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] boll = Arrays.stream(in.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
        int[] order = Arrays.stream(in.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
        System.out.println(getResult(boll, order));

    }

    public static String getResult(int[] boll, int[] order) {
        LinkedList<Integer> bucket = new LinkedList<>();
        StringBuilder ans = new StringBuilder();
        int j = 0;
        for (int i = 0; i < boll.length; i++) {
            bucket.add(boll[i]);
            while (!bucket.isEmpty()) {
                int left = bucket.peekFirst();
                int right = bucket.peekLast();
                if (left == order[j]) {
                    bucket.pollFirst();
                    ans.append(&quot;L&quot;);
                    j++;
                } else  if (right == order[j]) {
                    bucket.pollLast();
                    ans.append(&quot;R&quot;);
                    j++;
                } else
                    break;
            }
        }
        return ans.toString();
    }
全部评论

相关推荐

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