其他分享
首页 > 其他分享> > Leetcode 394 字符串解码 用栈处理层级结构

Leetcode 394 字符串解码 用栈处理层级结构

作者:互联网

  C 手写栈结构:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "stdbool.h"

struct Node
{
    char val;
    int num;
    struct Node *next;
    struct Node *pre;
};

struct Stack
{
    struct Node *head;
    struct Node *last;
    int len;
};

struct Stack *createStack()
{
    struct Stack *stack = (struct Stack *)malloc(sizeof(struct Stack));
    stack->head = (struct Node *)malloc(sizeof(struct Node));
    stack->last = stack->head;
    stack->head->pre = NULL;
    stack->head->next = NULL;
    stack->head->val = NULL;
    stack->len = 0;
    return stack;
}

void push(char c, struct Stack *stack)
{
    struct Node *node = (struct Node *)malloc(sizeof(struct Node));
    node->val = c;
    node->num = NULL;
    node->next = NULL;
    stack->last->next = node;
    node->pre = stack->last;
    stack->last = node;
    (stack->len)++;
}

void pushNum(int num, struct Stack *stack)
{
    struct Node *node = (struct Node *)malloc(sizeof(struct Node));
    node->val = NULL;
    node->num = num;
    node->next = NULL;
    stack->last->next = node;
    node->pre = stack->last;
    stack->last = node;
    (stack->len)++;
}

char pop(struct Stack *stack)
{
    if (stack->len == 0)
        return NULL;
    struct Node *node = stack->last;
    stack->last = stack->last->pre;
    stack->last->next = NULL;
    (stack->len)--;
    return node->val;
}

int popNum(struct Stack *stack)
{
    if (stack->len == 0)
        return NULL;
    struct Node *node = stack->last;
    stack->last = stack->last->pre;
    stack->last->next = NULL;
    (stack->len)--;
    return node->num;
}

void freeStack(struct Stack *stack)
{
    struct Node *node = stack->head;
    while (node->next != NULL)
    {
        struct Node *preNode = node;
        node = node->next;
        free(preNode);
    }
    free(stack);
}

char *toString(struct Stack *stack)
{
    int len = stack->len + 1;
    char *re = (char *)malloc(sizeof(char) * (len));
    memset(re, '\0', len * sizeof(char));
    struct Node *node = stack->head->next;
    int point = 0;
    while (node != NULL)
    {
        re[point++] = node->val;
        node = node->next;
    }
    return re;
}

void pushStack(struct Stack *stack0, struct Stack *stack1)
{
    if (stack0->len == 0)
        return;
    struct Node *node = stack0->last;
    while (node != NULL && node->val != NULL)
    {
        push(node->val, stack1);
        node = node->pre;
    }
}

bool isNum(char c)
{
    return c >= '0' && c <= '9';
}

int sumT(int t)
{
    int re = 1;
    for (int i = 0; i < t; i++)
    {
        re *= 10;
    }
    return re;
}

char *decodeString(char *s)
{
    int len = strlen(s);
    struct Stack *stack = createStack();
    for (int i = 0; i < len; i++)
    {
        char c = s[i];
        if (isNum(c))
        {
            int point = i + 1;
            while (s[point] != '[')
                point++;
            int num = 0, t = 0;
            for (int j = point - 1; j >= i; j--)
            {
                num += sumT(t) * (s[j] - 48);
                t++;
            }
            pushNum(num, stack);
            i = point - 1;
        }
        else if (c == ']')
        {
            struct Stack *inStack = createStack();
            char currentC = NULL;
            while ((currentC = pop(stack)) != '[')
                push(currentC, inStack);
            int num = popNum(stack);
            for (int j = 0; j < num; j++)
            {
                pushStack(inStack, stack);
            }
            freeStack(inStack);
        }
        else
            push(c, stack);
    }
    return toString(stack);
}

  JAVA:

public final String decodeString(String s) {
        Stack<Character> stack = new Stack<Character>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (isNum(c)) {
                int right = i + 1;
                while (s.charAt(right) != '[') right++;
                String numStr = s.substring(i, right);
                int num = Integer.valueOf(numStr);
                stack.push((char) num);
                i = right - 1;
            } else if (c == ']') {
                StringBuilder stringBuilder = new StringBuilder();
                char popC;
                while ((popC = stack.pop()) != '[') {
                    stringBuilder.append(popC);
                }
                int num = (int) stack.pop();
                for (int j = 0; j < num; j++) {
                    for (int k = stringBuilder.length() - 1; k >= 0; k--)
                        stack.push(stringBuilder.charAt(k));
                }
            } else stack.push(c);
        }
        String re = "";
        for (int i = 0; i < stack.size(); i++) re += stack.get(i);
        return re;
    }


    private boolean isNum(char c) {
        int asc = (int) c;
        return c >= 48 && c <= 58;
    }

  JS:

/**
 * @param {string} s
 * @return {string}
 */
var decodeString = function (s) {
    let stack = [];
    for (let i = 0; i < s.length; i++) {
        let c = s.charAt(i);
        if (isNumber(c)) {
            let point = i + 1, currentNum = null;
            while ((currentNum = s.charAt(point)) != '[') {
                c += currentNum;
                point++;
            }
            stack.push(c);
            i = point - 1;
        } else if (c == ']') {
            let currentRe = [], currentC = null;
            while ((currentC = stack.pop()) != '[') currentRe.push(currentC);
            let num = stack.pop();
            for (let j = 0; j < num; j++) {
                for (let k = currentRe.length - 1; k >= 0; k--) stack.push(currentRe[k]);
            }
        } else stack.push(c);
    }
    let re = "";
    for (let i = 0; i < stack.length; i++) re += stack[i];
    return re;
};

function isNumber(val) {
    var regPos = /^[0-9]+.?[0-9]*/; //判断是否是数字。
    if (regPos.test(val)) return true;
    else return false;
}

 

标签:node,Node,last,struct,int,Leetcode,用栈,394,stack
来源: https://www.cnblogs.com/niuyourou/p/16217160.html