icoding块链串(改进)

这篇文章也是在网上找了许多答案但都无法达到100%通过率的情况下写的。

以下是根据前辈的代码改进后实现100%通过率的代码(感谢网上前辈们提供的代码):

#include "dsstring.h" // 请不要删除,否则检查不通过
#include <stdio.h>
#include <stdlib.h>

bool blstr_substr(BLString src, int pos, int len, BLString* sub)
{
    if (pos < 0 || pos >= src.len || len < 1)
        return false;
    Block *p = (sub->head = (Block*)malloc(sizeof(Block))), *q = src.head;
    int i = 0, j = 0, k = 0;
    p->next = NULL;
    while (k <= pos + len - 1 && q && q->ch[i] != BLS_BLANK) {
        if (k < pos) {
            if (i < BLOCK_SIZE - 1)
                i++;
            else {
                q = q->next;
                i = 0;
            }
            k++;
        } else {
            p->ch[j] = q->ch[i];
            if (i < BLOCK_SIZE - 1)
                i++;
            else {
                q = q->next;
                i = 0;
            }
            if (j < BLOCK_SIZE - 1)
                j++;
            else {
                p->next = (Block*)malloc(sizeof(Block));
                p = p->next;
                p->next = NULL;
                j = 0;
            }
            k++;
            sub->len++;
        }
    }
    if (j) {
        sub->tail = p;
        while (j < BLOCK_SIZE)
            p->ch[j++] = BLS_BLANK;
    } else {
        for (sub->tail = sub->head; sub->tail->next != p; sub->tail = sub->tail->next)
            ;
        sub->tail->next = NULL;
        free(p);
    }
    return true;
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值