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. Linux使用退格键时出现^H + Tab键命令补全失效/方向键失效 + ls文件夹和文件没有颜色

    删除问题 安装kalilinux使用普通用户的的时候按退格键无法实现删除功能 解决的办法有两个 一改变快捷键: 使用Ctrl+Backspace组合键可以实现删除功能 ctrl + backspace ...

  2. MFC中的CString类使用方法指南

    MFC中的CString类使用方法指南 原文出处:codeproject:CString Management [禾路:这是一篇比较老的资料了,但是对于MFC的程序设计很有帮助.我们在MFC中使用字符 ...

  3. shell 调试脚本设置

    set -x 脚本部分内容 set +x

  4. Python中对象的引用与复制

    在python进行像b = a这样的赋值时,只会创建一个对a的新引用,使a的引用计数加1,而不会创建新的对象: >>> a = 'xyz' >>> import s ...

  5. XcodeProj,使用Ruby更改工程文件

    利用xcodeproj修改xcode工程文件 一,Ruby基础 Ruby迭代器each.map.collect.inject each——连续访问集合的所有元素collect—-从集合中获得各个元素传 ...

  6. Mac安装HomeBridge适配小米Homekit报错:module未找到解决

    Mac安装HomeBridge适配小米Homekit报错:module未找到 具体错误是: module.js:471 throw err; balabalal...... 问了一圈,终于解决,但是又 ...

  7. Oracle常用函数——TO_DATE

    TO_DATE 含义:将具有固定格式的字符串类型的数据转化为相对应的Date类型数据,官网解释如下图   使用方法 TO_DATE("需要转换的字符串","日期格式&qu ...

  8. Bootstrap3基础 btn-primary/warning... 三类按钮的六种样式

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  9. Python3 tkinter基础 Entry validatecommand 获取输入框的值

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  10. 如何查看linux程序被何种版本的编译器编译的?

    答: 使用vi工具之间搜索关键字"GCC"即可找出编译该程序的编译器版本号!