解码字符串 Decode String
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的更多相关文章
- [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 ...
- LeetCode 394. 字符串解码(Decode String) 44
394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...
- [LeetCode] Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- [LeetCode] 394. Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- [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 ...
- [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 ...
- [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 ...
- Python基础数据类型-字符串(string)
Python基础数据类型-字符串(string) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版 ...
- Java基础-字符串(String)常用方法
Java基础-字符串(String)常用方法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.java的API概念 Java的API(API:Application(应用) Pr ...
随机推荐
- mysql服务器iowait高优化一例完整深入解析
我们有一服务器,上面运行着两个mysql实例,这几天iowait一直很高,在20-30%,下午特地专门排查和解决了下,相关过程整理如下. 该服务器有两个挂载盘,服务器在阿里云上,一个系统盘,一个数据盘 ...
- mysql Out of range value adjusted for column导致Warning(1265)Data truncated for column 'column_name' at row 1
今天下午,我们的一个开发来找我,说线上有个环境报了"Warning(1265)Data truncated for column 'column_name' at row 1",定 ...
- Docker学习记录--入门了解+安装
Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制, ...
- opencv学习之路(17)、边缘检测
一.概述 二.canny边缘检测 #include "opencv2/opencv.hpp" using namespace cv; void main() { //Canny边缘 ...
- css 元素居中
css 4种常见实现元素居中的办法: 1.通过 margin 属性调整 : { position: absolute; top: 50%; left: 50%; margin-left: 盒子的一半: ...
- 装了as之后提示NO JVM installation found.....
如图. 解决:在AS安装目录下,找到对应的程序[jdk是多少位就打开多少位的]
- git克隆源码时提示fatal: HTTP request failed怎么办?
答: 升级git版本即可 centos下升级git的方法在此
- centos6.8下如何升级git版本?
1. 安装开发环境 yum install -y curl curl-devel zlib-devel openssl-devel perl cpio expat-devel gettext-deve ...
- ThreadLocal 的机制与内存泄漏
ThreadLocal笔记 如上图所示 每个Thread 都有一个map,里面存着Entry<Key,value>,而key是实现了WeakReference的ThreadLocal,如果 ...
- Trimmomatic过滤Illumina低质量序列
1. 下载安装 直接去官网下载二进制软件,解压后的trimmomatic-0.36.jar即为我们需要的软件 官网: http://www.usadellab.org/cms/index.php?pa ...