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 ...
随机推荐
- 我的cnblogs设置代码
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- md RAID
md: mdadm: 将任何块设备做成RAID 模式化的命令:1.创建模式 -C 专用选项: -l: 级别 -n #: 设备个数 -a {yes|no}: 是否自动为其创建设备文件 -c: CHUNK ...
- ViewController 优化
解决问题:部分复杂页面的Controller过于庞大,不利于维护与复用: 复杂的页面大多是基于tableview的页面.复杂页面的代码大致可分为两部分(复杂的View布局用Nib实现的话,一般大家都是 ...
- PhyreEngine3.8 MSAA resolution
There is something wrong in PhyreEngine 3.8 to fullfill MSAA, actually, I think it is eqaa You have ...
- 数组对象Vector用法
import java.util.Vector; public class VectorTesting { public static void main(String[] args) { Vecto ...
- windows service 安装和卸载指令
添加服务: cd C:\Windows\Microsoft.NET\Framework\v4.0.30319InstallUtil.exe D:\OneKeyWebSiteDeployment\Ser ...
- codeforces 295E Yaroslav and Points (离线操作+离散化+区间合并)
参考链接:http://blog.csdn.net/dyx404514/article/details/8817717 写的很详细,这里就不再赘述,附上我的代码. #include <iostr ...
- Test Markdown Editor
Last night, I just saw a cute blogger's homepage. Then I want to write something. But anyway, I use ...
- nmap使用详解
nmap是一个网络探测和安全扫描程序, 系统管理者和个人可以使用这个软件扫描大型的网络,获取那台主机正在运行以及提供什么服务等信息.nmap支持很多扫描技术,例如:UDP.TCP connect(). ...
- Meteor 简介
简介 先来活动一下大脑.假设你坐在电脑面前,在两个窗口中打开同一个文件夹. 在其中一个窗口中删除一个文件,另一个窗口中的这个文件会消失吗? 不用实际操作你也知道肯定会消失的.在本地文件系统中的操作,不 ...