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 ...
随机推荐
- Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- JPA学习---第四节:JPA实例与JPA主键生成策略
1.编写实体类,代码如下: package learn.jpa.bean; import javax.persistence.Entity; import javax.persistence.Gene ...
- C++(MFC)编程一些注意事项
一·书写问题 1.括号:左右大括号最好都放在左侧,这样可以很清楚大括号的看清配对情况以及作用域,便于检查也不易出错. 2.强制转换:强制转换表达式时一定要加括号,否则可能只转换了表达式中的单个量,可能 ...
- android 开发上传图片遇到返回 FileNotFoundException
目的:带有参数上传图片 环境:客户端开发 android studio ,服务器端:visual studio 2012 (c# asp.net) ************************* ...
- javascript_22_for_js性感的v
<script type="text/javascript"> window.onload=function(){ var aDiv=document.getEleme ...
- MVC3+AutoFac实现程序集级别的依赖注入
1.介绍 所谓程序集级别的依赖注入是指接口和实现的依赖不使用配置文件或硬代码实现(builder.RegisterType<UserInfoService>().As<IU ...
- redis window环境下的安装地址
https://github-cloud.s3.amazonaws.com/releases/3402186/25358446-c083-11e5-89cb-61582694855e.zip?X-Am ...
- 【设计模式六大原则1】单一职责原则(Single Responsibility Principle)
http://blog.csdn.net/zhengzhb/article/category/926691/1 图片素材来源,java学习手册 ps.内容为自己整理 定义:不要存在多于一个 ...
- ZOJ 3261 Connections in Galaxy War(逆向并查集)
参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...
- java基础知识回顾之---java String final类普通方法
辞职了,最近一段时间在找工作,把在大二的时候学习java基础知识回顾下,拿出来跟大家分享,如果有问题,欢迎大家的指正. /* * 按照面向对象的思想对字符串进行功能分类. * ...