华为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("L");
j++;
} else if (right == order[j]) {
bucket.pollLast();
ans.append("R");
j++;
} else
break;
}
}
return ans.toString();
}
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("L");
j++;
} else if (right == order[j]) {
bucket.pollLast();
ans.append("R");
j++;
} else
break;
}
}
return ans.toString();
}
全部评论
相关推荐
点赞 评论 收藏
分享