decode-string(挺麻烦的)
Java String作为参数传参是不会改变的,这个与常识的感觉不同。
public String decodeString(String s) {
s = "";
return s;
} String s = "3[a2[c]]";
String ret = solution.decodeString(s);
System.out.printf("Get ret: %s\n", s); 结果:
Get ret: 3[a2[c]]
下面是正题和解法:
https://leetcode.com/problems/decode-string/ // 唉,还是做的太慢了 package com.company; import java.util.*; class Solution {
class Ret {
String str;
int pos;
}
String s;
int slen; Ret parse(int index) {
// 只能以数字或者字母开头
Ret ret = new Ret();
StringBuilder sb = new StringBuilder(); while (index < slen && s.charAt(index) >= 'a' && s.charAt(index) <= 'z') {
sb.append(s.charAt(index));
index++;
}
String prefix = sb.toString(); while (index < slen) { if (s.charAt(index) == ']') {
break;
} int multi = 0;
while (index < slen && s.charAt(index) >= '0' && s.charAt(index) <= '9') {
multi = multi * 10 + s.charAt(index) - '0';
index++;
} //System.out.println("start pos is " + index + " multi " + multi); if (s.charAt(index) == '[') {
Ret tmpRet = parse(index+1);
sb = new StringBuilder();
for (int i = 0; i < multi; i++) {
sb.append(tmpRet.str);
}
index = tmpRet.pos + 1;
//System.out.println("pos is " + index + " multi " + multi + " sb " + sb.toString());
}
else {
sb = new StringBuilder();
while (index < slen && s.charAt(index) >= 'a' && s.charAt(index) <= 'z') {
sb.append(s.charAt(index));
index++;
}
} prefix += sb.toString();
//System.out.println("prefix is " + prefix); } //System.out.println("index" + index + " prefix" + prefix);
ret.str = prefix;
ret.pos = index; return ret;
} public String decodeString(String s) {
this.s = s;
this.slen = s.length();
Ret ret = parse(0);
return ret.str;
}
} public class Main { public static void main(String[] args) {
System.out.println("Hello!");
Solution solution = new Solution(); String s = "2[abc]3[cd]ef";
String ret = solution.decodeString(s);
System.out.printf("Get ret: %s\n", ret); System.out.println(); }
} // 以下是原来的 public class Solution {
private String s;
private int newPos; public String decodeString(String ins) {
s = '.' + ins + ']';
newPos = 0;
String outStr = impl(1, 0);
return outStr.substring(1, outStr.length());
} private String impl(int prefix, int startPos) {
int base = 0;
String baseStr = "";
String outStr = ""; for (int i=startPos; i<s.length(); i++) {
char ch = s.charAt(i); if (ch == '[') {
int tmpPos = i+1;
baseStr += impl(base, tmpPos);
i = newPos;
base = 0;
}
else if (ch == ']') {
for (int j=0; j<prefix; j++) {
outStr += baseStr;
}
// At begin, use i+1, is wrong,
// because in each loop there's i++
newPos = i;
return outStr;
}
else if (!Character.isDigit(ch)){
baseStr += ch;
}
else {
base = base * 10 + ch - '0';
}
} return outStr;
}
}
decode-string(挺麻烦的)的更多相关文章
- UVA 110 Meta-Loopless Sorts(输出挺麻烦的。。。)
Meta-Loopless Sorts Background Sorting holds an important place in computer science. Analyzing and ...
- [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 394. 字符串解码(Decode String) 44
394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...
- [Swift]LeetCode394. 字符串解码 | Decode String
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- 394. Decode String
[题目] Total Accepted: 10087 Total Submissions: 25510 Difficulty: Medium Contributors: Admin Given an ...
- [LeetCode] Decode String 题解
题目 题目 s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return " ...
- Decode String
Given an encoded string, return it's decoded string.The encoding rule is: k[encoded_string], where t ...
- Leetcode -- 394. Decode String
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
随机推荐
- 服务器端spice配置详解
1. 安装必要的工具 sudo apt-get install build-essential autoconf git-core intltool 2. 安装必要的依赖包 -dev libxfixe ...
- OpenWrt编译到底脚本
在办公室编译OpenWrt,费时很久,原因有两个. 一是办公室网络环境比较糟糕,经常断线不说,很多技术网站间歇性的连不上,不是撞到404就是DNS解析失败等. 二是初次编译OpenWrt时需要从网上下 ...
- GitHub error “Failed to get HEAD”
cd /要提交的文件的文件夹下 比如要提交一个名为 demo的 程序, 那么先进入demo 的文件夹里面 然后 进行以下两步 git init (有时这个是不必要的,因为xcode 自 ...
- 查看Linux磁盘空间大小
一.df 命令: df 是来自于coreutils 软件包,系统安装时,就自带的:我们通过这个命令可以查看磁盘的使用情况以及文件系统被挂载的位置: 举例: [root@localhost beinan ...
- WPF 多线程处理(3)
WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 首先我们需要几个属性来保存取得的数据,因为在 ...
- org.apache.commons.dbutils.QueryRunner 执行sqlserver的存储过程
执行不带输出参数的存储过程与 执行普通update sql没有什么区别,直接调用即可: 示例代码: public Boolean startResidentialInfoStatistics(Str ...
- 【转】KM匹配题集
转自:http://blog.csdn.net/shahdza/article/details/7779324 [HDU]2255 奔小康赚大钱 模板题★1533 Going Home 模板题★242 ...
- Matlab找二维数组最大值
1.m先max(x)求出每列最大值,返回行向量,再max对行向量求出最大值,就是max(max(x)). 注意:max(x),不管x是行列向量,只要是向量,那么就返回一个值. 2.先x(:)转为按列的 ...
- PHP之list()函数讲解
定义和用法 list() 函数用数组中的元素为一组变量赋值. 注意,与 array() 类似,list() 实际上是一种语言结构,不是函数. 语法 list(var1,var2...) 参数 描述 v ...
- 用Ant实现Java项目的自动构建和部署(转)
Ant是一个Apache基金会下的跨平台的构件工具,它可以实现项目的自动构建和部署等功能.在本文中,主要让读者熟悉怎样将Ant应用到Java项目中,让它简化构建和部署操作. 一. ...