道阻且长,行则将至,走慢一点没关系,不停下就|

Ac_c0mpany丶

园龄:3年7个月粉丝:6关注:3

2023-12-24 17:09阅读: 3评论: 0推荐: 0

[LeetCode Hot 100] LeetCode394. 字符串解码

题目描述

思路


思路:

  • 碰到数字:压入数字栈,注意多位数的情况
  • 碰到字母:直接拼接到res
  • 遇到[:将num和res分别压入栈
  • 遇到]:开始处理栈顶元素

方法一:

class Solution {
    public String decodeString(String s) {
        int num = 0;
        StringBuilder res = new StringBuilder();
        Deque<String> DigStack = new ArrayDeque<>();
        Deque<Integer> NumStack = new ArrayDeque<>();

        for (char c : s.toCharArray()) {
            if (c >= '0' && c <= '9') {
                num = num * 10 + (c - '0');
            } else if (c == '[') {
                NumStack.push(num);
                DigStack.push(res.toString());
                res.delete(0, res.length());
                num = 0;
            } else if (c == ']') {
                String item = res.toString();
                int times = NumStack.pop();
                for (int i = 0; i < times - 1; i ++) {
                    res.append(item);
                }
                res.insert(0, DigStack.pop());
            } else{
                res.append(c);
            }
        }
        return res.toString();
    }
}

本文作者:Ac_c0mpany丶

本文链接:https://www.cnblogs.com/keyongkang/p/17924616.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ac_c0mpany丶  阅读(3)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 You Are My Sunshine REOL
You Are My Sunshine - REOL
00:00 / 00:00
An audio error has occurred.

作曲 : Traditional

You are my sunshine

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away

The other night, dear,

When I lay sleeping

I dreamed I held you in my arms.

When I awoke, dear,

I was mistaken

So I hung my head and cried.

You are my sunshine,

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away.

You are my sunshine,

My only sunshine

You make me happy

When skies are gray.

You'll never know, dear

How much I love you

Please don't take my sunshine away

Please don't take my sunshine away.

Please don't take my sunshine away.