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. 12: xlrd 处理Excel文件

    1.1 xlrd处理.xlsx 文件 1.xlrd常用方法 #!/usr/bin/python # coding:utf-8 # 用xlrd读取Excel文件基本用法 import sys impor ...

  2. 使用UI管理docker

    比较全面的对比 wangzi19870227 比较推荐的有两种(http://dockone.io/article/225): Shipyard 和 dockerui https://docs.doc ...

  3. c++map用法

    #include<iostream> #include<algorithm> #include<map> using namespace std; void fun ...

  4. bzoj 2527: [Poi2011]Meteors

    昨天写了一晚,越写复杂度越感觉不对,早上一想果然是假的. (这里n,m,k我就不区分了) 首先一个城市的询问可以很容易的二分 check用树状数组维护区间(区间修改,单点查询的那种) 一次是\(O(n ...

  5. 初识Java框架

    Spring boot>spring>spring mvc SSH:struts2+spring+hibernate SSM(SSH的改进): (过去)spring+struts2+MyB ...

  6. (转载)MySQL用命令行复制表的方法

    mysql中用命令行复制表结构的方法主要有一下几种: 1.只复制表结构到新表 ; 或 CREATE TABLE 新表 LIKE 旧表 ; 注意上面两种方式,前一种方式是不会复制时的主键类型和自增方式是 ...

  7. Nginx配置示例

    server {listen 6080;server_name local.boheadmin; location / {proxy_pass http://127.0.0.1:8087;} loca ...

  8. Jenkins简介

    Jenkins 是一个开源项目,提供了一种易于使用的持续集成系统,使开发者从繁杂的集成中解脱出来,专注于更为重要的业务逻辑实现上.同时 Jenkins 能实施监控集成中存在的错误,提供详细的日志文件和 ...

  9. WijmoJS 全面支持 Angular 7

    概述 首先恭喜Angular团队发布Angular 7.0.0版本! 对于大多数开发人员,只需要执行一个命令就可以更新到Angular 7: ng update \@angular/cli \@ang ...

  10. linux 基本命令2(12月27日笔记)

    1.ifconfig 作用:用于操作网卡相关的指令 简单语法:#ifconfig      (获取网卡信息)   2.reboot 作用:重新启动计算机 语法1:#reboot             ...