【leetcode_838】【中等】ush-dominoes / 推多米诺

URL

链接:https://leetcode-cn.com/problems/push-dominoes/


题目

请添加图片描述

请添加图片描述


分析

请添加图片描述


源码

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <stdbool.h> //for bool

typedef struct StListNode{
    int val;
    struct StListNode * next ;
}StListNode;

typedef struct Queue{
    struct StListNode * head;
    struct StListNode * tail;
    int length;
}Queue;

bool isEmpty(const Queue * obj){
    return obj->length == 0;
}

int length(const Queue *obj){
    return obj->length;
}

bool initQueue(Queue * obj){
    obj->head = NULL;
    obj->tail = NULL;
    obj->length = 0;
    return true;
}

bool pushQueue(Queue * obj, int val){
    StListNode * node = (StListNode *)malloc(sizeof(StListNode));
    node->val = val;
    node->next = NULL;
    if(NULL == obj->head){
        obj->head = node;
        obj->tail = node;
    }else{
        obj->tail->next = node;
        obj->tail = obj->tail->next;
    }
    obj->length++;
    return true;
}

int front(const Queue * obj){
    if (obj->length == 0){
        return -1;
    }
    return obj->head->val;
}
int popQueue(Queue * obj){
    if (obj->length == 0){
        return -1;
    }
    int res = obj->head->val;
    StListNode * node = obj->head;
    obj->head = obj->head->next;
    obj->length--;
    free(node);
    return res;
}

char * pushDominoes(char * dominoes){
    int n = strlen(dominoes);
    int * time = (int *)malloc(sizeof(int) * n);
    char * res = (char *)malloc(sizeof(char) * (n + 1));
    Queue ** force = (Queue **)malloc(sizeof(Queue*) * n);

    for (int i = 0; i < n; i++){
        time[i] = -1;
        force[i] = (Queue *)malloc(sizeof(Queue));
        initQueue(force[i]);
        res[i] = '.';
    }

    res[n] = '\0';
    Queue qu;
    initQueue(&qu);
    for (int i = 0; i < n; i++){
        if (dominoes[i] != '.'){
            pushQueue(&qu, i);
            time[i] = 0;
            pushQueue(force[i], dominoes[i]);
        }
    }

    while(!isEmpty(&qu)){
        int i = popQueue(&qu);
        if (length(force[i]) == 1){
            char f = front(force[i]);
            res[i] = f;
            int ni = (f == 'L')?(i - 1) : (i + 1);
            if(ni >= 0 && ni < n){
                int t = time[i];
                if (time[ni] == -1){
                    pushQueue(&qu, ni);
                    time[ni] = t + 1;
                    pushQueue(force[ni], f);
                } else if (time[ni] == t + 1){
                    pushQueue(force[ni], f);
                }
            }
        }
    }
    for (int i = 0; i < n; i++){
        while(!isEmpty(force[i])){
            popQueue(force[i]);
        }
    }
    return res;
}

int main()
{

    char dominoes_1[] = "RR.L";//input dominoes = "RR.L" ; output "RR.L"
    printf("before : %s\n",dominoes_1);
    printf("after  : %s\n",pushDominoes(dominoes_1));

    char dominoes_2[] = ".L.R...LR..L..";//input dominoes = dominoes = ".L.R...LR..L.." ; output "LL.RR.LLRRLL.."
    printf("before : %s\n",dominoes_2);
    printf("after  : %s\n",pushDominoes(dominoes_2));

    return 0;
}

before : RR.L
after  : RR.L
before : .L.R...LR..L..
after  : LL.RR.LLRRLL..

源码概述

官方给了广度优先的解题思路。
比较经典,不再赘述


小结

用正反遍历模拟的思路可能更适合这一题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

过得精彩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值