多米诺骨牌(模拟)

  • 初始化数据结构

    • 使用一个布尔数组 arr 来表示每个位置是否被占用。初始时所有位置均为 false(未占用)。
    • 使用一个 LinkedHashMap(命名为 queue)来记录最近的 R 操作的位置。这个结构可以保持插入顺序,方便后续处理。
  • 遍历输入字符串

    • 遍历每个字符,根据字符的类型(.LR)进行不同的处理:
      • .:表示空位,跳过。
      • L
        • 如果 queue 为空(没有 R),将当前位置之前的所有位置标记为占用(true)。
        • 如果 queue 不为空,处理最近的 R
          • queue 中获取并移除最近的 R 的位置。
          • 计算从这个 R 到当前 L 之间的影响区域,并根据位置关系决定标记的方式。具体来说,如果 LR 之间的距离是偶数,则需要跳过中间位置;如果是奇数,则可以直接标记所有位置为占用。
      • 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"));
      }

      }

相关推荐
To_OC5 小时前
LC 131 分割回文串:刚学回溯时,我连怎么切字符串都想不明白
javascript·算法·leetcode
旖-旎6 小时前
LeetCode 518:零钱兑换||(完全背包)—— 题解
c++·算法·leetcode·动态规划·背包问题
To_OC6 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode
delishcomcn7 小时前
AI视觉识别+分切算法:电化铝缺陷检测与裁切一体化解锁
人工智能·算法
触底反弹7 小时前
深入理解大模型采样:Temperature、Top-K、Top-P 的原理与实战
人工智能·算法·面试
雪碧聊技术8 小时前
力扣 LCR 091. 粉刷房子 —— 动态规划入门详解
算法·动态规划
延凡科技10 小时前
多场景落地复盘:端边云架构无人机智能巡检系统设计与实践
大数据·数据结构·人工智能·科技·架构·无人机·能源
CV-Climber11 小时前
检索技术的实际应用
人工智能·算法
hhzz11 小时前
Tiger AI Platform平台中增加人脸识别功能
图像处理·人工智能·算法·计算机视觉·大模型