2018-11-14 17:56:12

问题描述:

问题求解:

方法一、递归求解

最直观的解法就是递归来求了,并且很显然的这个问题可以使用递归来进行求解。

    public String decodeString(String s) {
StringBuffer sb = new StringBuffer();
int num = 0;
for (int i = 0; i < s.length(); i++) {
if (Character.isDigit(s.charAt(i))) {
num = 0;
while (i < s.length() && Character.isDigit(s.charAt(i))) {
num = num * 10 + (s.charAt(i) - '0');
i++;
}
i--;
}
else if (s.charAt(i) == '[') {
int count = 1;
int j = i;
while (count != 0) {
j++;
if (s.charAt(j) == '[') count++;
if (s.charAt(j) == ']') count--;
}
String tmp = decodeString(s.substring(i + 1, j));
for (int k = 1; k < num; k++) sb.append(tmp);
i = j;
}
else sb.append(s.charAt(i));
}
return sb.toString();
}

方法二、使用Stack求解

使用Stack求解的时候,最核心的思路就是当遇到'['的时候将当前sb 和 num进行压栈操作,当遇到‘]’的时候将堆栈中sb 和 num取出并对当前的字符串进行num次重复并串联到sb后面。

    public String decodeString(String s) {
StringBuffer sb = new StringBuffer();
Stack<StringBuffer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
int k = 0;
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) k = k * 10 + c - '0';
else if (c == '[') {
stack1.push(sb);
stack2.push(k);
sb = new StringBuffer();
k = 0;
}
else if (c == ']') {
int count = stack2.pop();
String tmp = sb.toString();
sb = stack1.pop();
for (int i = 0; i < count; i++) sb.append(tmp);
}
else sb.append(c);
}
return sb.toString();
}

解码字符串 Decode String的更多相关文章

  1. [Swift]LeetCode880. 索引处的解码字符串 | Decoded String at Index

    An encoded string S is given.  To find and write the decodedstring to a tape, the encoded string is ...

  2. LeetCode 394. 字符串解码(Decode String) 44

    394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...

  3. [LeetCode] Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  4. [LeetCode] 394. Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  5. [LeetCode] Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  6. [Swift]LeetCode271. 加码解码字符串 $ Encode and Decode Strings

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  7. [LeetCode] 271. Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  8. Python基础数据类型-字符串(string)

    Python基础数据类型-字符串(string) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版 ...

  9. Java基础-字符串(String)常用方法

    Java基础-字符串(String)常用方法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.java的API概念 Java的API(API:Application(应用) Pr ...

随机推荐

  1. Codeforces 808G Anthem of Berland - KMP - 动态规划

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定一个字符串$s$,和一个字符串$t$,$t$只包含小写字母,$s$包含小写字母和通配符'?'.询问$t$可能在$s$中出现最多多少次. 原 ...

  2. 尚硅谷面试第一季-12Linux常用服务类相关命令

    课堂重点: 实操命令及运行结果: (centos 6) service network status chkconfig --list chkconfig --level 5 network off ...

  3. django基础 -- 6. 多表操作

    一.多表的创建 from django.db import models # Create your models here. class Author(models.Model): id = mod ...

  4. 打造性感好用的 VS Code 编辑器

    官网: https://code.visualstudio.com/ Blog链接:打造性感好用的VS Code编辑器 主命令框 F1或Ctrl+Shift+P: 打开命令面板.在打开的输入框内,可以 ...

  5. centos在图形界面和命令行之间切换的快捷键是什么?

    答: ctrl+alt+F1 或者ctrl+alt+F2 1.当前处于图形界面时,按ctrl+alt+F2可进入命令行模式 2. 当前处于命令行模式,按ctrl+alt+F1可进入图形界面

  6. hihoCoder week11 树中的最长路

    题目链接: https://hihocoder.com/contest/hiho11/problem/1 求树中节点对 距离最远的长度 #include <bits/stdc++.h> u ...

  7. spring 配置ibatis和自动分页

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  8. CentOS7时间和日期的同步 (chrony和)

    CentOS 6版本,使用 hwclock CentOS 7版本,使用timedatectl 1.基本概念 1.1 GMT,UTC,CST,DST时间 世界标准时间 整个地球分为二十四时区,每个时区都 ...

  9. 良品铺子:“新零售”先锋的IT必经之路

    良品铺子:“新零售”先锋的IT必经之路 云计算 大数据 CIO班 CIO 互联网+ 物联网 电子政务 2017-12-29 09:25:34  来源:互联网抢沙发 摘要:2017年被称为“新零售”元年 ...

  10. 面试官:你了解Webpack吗?

    前言 大家好哟,这是第四篇面试官篇,估计还有个七八十篇面试文章(前端苦命). 这篇文章介绍了webpack核心概念以及如何使用. 开始吧! 概念 webpack的核心概念只要记住下面四个就够用了(除非 ...