-
初始化数据结构:
- 使用一个布尔数组
arr来表示每个位置是否被占用。初始时所有位置均为false(未占用)。 - 使用一个
LinkedHashMap(命名为queue)来记录最近的R操作的位置。这个结构可以保持插入顺序,方便后续处理。
- 使用一个布尔数组
-
遍历输入字符串:
- 遍历每个字符,根据字符的类型(
.、L、R)进行不同的处理:.:表示空位,跳过。L:- 如果
queue为空(没有R),将当前位置之前的所有位置标记为占用(true)。 - 如果
queue不为空,处理最近的R:- 从
queue中获取并移除最近的R的位置。 - 计算从这个
R到当前L之间的影响区域,并根据位置关系决定标记的方式。具体来说,如果L和R之间的距离是偶数,则需要跳过中间位置;如果是奇数,则可以直接标记所有位置为占用。
- 从
- 如果
R:将当前索引加入queue,以备后续处理。
- 遍历每个字符,根据字符的类型(
-
处理剩余的
R:- 遍历完字符串后,如果
queue中还有R,取出第一个R的位置,将这个位置及其后所有位置标记为占用。
- 遍历完字符串后,如果
-
计算未占用的位置:
- 遍历
arr数组,统计未被占用的位置,并将它们的索引(1-based)加入结果队列。
- 遍历
-
构造结果字符串:
-
如果没有未占用的位置,返回
"0"。 -
否则,构造结果字符串,格式为
"count:pos1,pos2,...",并返回。import java.util.ArrayDeque;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Iterator;public class Main {
public static String solution(int num, String data) {
boolean[] arr = new boolean[data.length()];
LinkedHashMap<Character, Integer> queue = new LinkedHashMap<>();for (int i = 0; i < data.length(); i++) { switch (data.charAt(i)) { case '.': break; case 'L': if (queue.isEmpty()) { for (int j = 0; j <= i; j++) { arr[j] = true; } } else { Iterator<Map.Entry<Character, Integer>> iterator = queue.entrySet().iterator(); Map.Entry<Character, Integer> firstEntry = iterator.next(); // 获取第一个条目 iterator.remove(); boolean skipTwo = false; int top = firstEntry.getValue(); int extra = (i + top) / 2; if ((i - top) % 2 != 0) { skipTwo = true; } for (int j = top; j <= i; j++) { if (skipTwo) { arr[j] = true; } else { if (j != extra) { arr[j] = true; } } } } break; case 'R': queue.put('R', i); break; } } // Check if the queue is not empty if (!queue.isEmpty()) { // Retrieve and remove the first entry Iterator<Map.Entry<Character, Integer>> iterator = queue.entrySet().iterator(); Map.Entry<Character, Integer> firstEntry = iterator.next(); iterator.remove(); // Pop the first entry if (firstEntry.getKey() == 'R') { int topValue = firstEntry.getValue(); for (int j = topValue; j < arr.length; j++) { arr[j] = true; // Set all subsequent elements to true } } } int count = 0; ArrayDeque<Integer> result = new ArrayDeque<>(); for (int i = 0; i < data.length(); i++) { if (!arr[i]) { count++; result.add(i + 1); // 1-based index } } if (count == 0) { return "0"; // All positions are filled } StringBuilder resultString = new StringBuilder(count + ":"); for (int pos : result) { resultString.append(pos).append(","); } resultString.setLength(resultString.length() - 1); // Remove the last comma return resultString.toString(); } public static void main(String[] args) { // // You can add more test cases here System.out.println(solution(14, ".L.R...LR..L..").equals("4:3,6,13,14")); System.out.println(solution(5, "R....").equals("0")); System.out.println(solution(1, ".").equals("1:1")); }}
-
多米诺骨牌(模拟)
%d%d22024-09-27 5:08
相关推荐
2301_764441332 小时前
LISA时空跃迁分析,地理时空分析东北洗浴王子讲AI2 小时前
GPT-5.4辅助算法设计与优化:从理论到实践的系统方法Billlly2 小时前
ABC 453 个人题解玉树临风ives2 小时前
atcoder ABC 452 题解feifeigo1233 小时前
基于马尔可夫随机场模型的SAR图像变化检测源码实现fengfuyao9853 小时前
基于STM32的4轴步进电机加减速控制工程源码(梯形加减速算法)无敌昊哥战神4 小时前
深入理解 C 语言:巧妙利用“0地址”手写 offsetof 宏与内存对齐机制小白菜又菜4 小时前
Leetcode 2075. Decode the Slanted CiphertextProxy_ZZ05 小时前
用Matlab绘制BER曲线对比SPA与Min-Sum性能黎阳之光5 小时前
黎阳之光:以视频孪生领跑全球,赋能数字孪生水利智能监测新征程