• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

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


Recommended Posts

utan0lj5ylb4109.png

C手写栈结构:

#包含标准库

#包含标准视频

#包含字符串。h

#include 'stdbool.h '

结构节点

{

字符值

整数

结构节点*下一个

结构节点*之前

};

结构堆栈

{

结构节点*头;

结构节点*最后

int len

};

结构堆栈*createStack()

{

struct Stack * Stack=(struct Stack *)malloc ((struct Stack)的大小);

stack-head=(结构节点*)malloc(sizeof(结构节点));

stack-last=栈头;

stack-head-pre=NULL;

stack-head-next=NULL;

stack-head-val=NULL;

stack-len=0;

返回堆栈;

}

空推(char c,结构堆栈*堆栈)

{

结构节点*节点=(结构节点*)malloc(sizeof(结构节点));

node-val=c;

node-num=NULL;

node-next=NULL;

stack-last-next=node;

节点前=栈-最后;

stack-last=节点;

(stack-len);

}

void pushNum(int num,struct Stack *stack)

{

结构节点*节点=(结构节点*)malloc(sizeof(结构节点));

node-val=NULL;

节点数量=数量;

node-next=NULL;

stack-last-next=node;

节点前=栈-最后;

stack-last=节点;

(stack-len);

}

字符弹出(结构堆栈*堆栈)

{

if (stack-len==0)

返回空

struct Node * Node=stack-last;

栈-最后=栈-最后-前;

stack-last-next=NULL;

(stack-len)-;

返回节点值;

}

int popNum(结构堆栈*堆栈)

{

if (stack-len==0)

返回空

struct Node * Node=stack-last;

栈-最后=栈-最后-前;

stack-last-next=NULL;

(stack-len)-;

返回节点号;

}

无效自由堆栈(结构堆栈*堆栈)

{

结构节点*节点=栈头;

而(节点-下一个!=空)

{

结构节点*前节点=节点

node=node-next;

免费(前节点);

}

免费(栈);

}

char *toString(结构堆栈*堆栈)

{

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; 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;
}

4bi0cb0hsda4110.png

vqut5dfzre04111.png

 

Link to comment
Share on other sites