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(挺麻烦的)的更多相关文章

  1. UVA 110 Meta-Loopless Sorts(输出挺麻烦的。。。)

     Meta-Loopless Sorts  Background Sorting holds an important place in computer science. Analyzing and ...

  2. [LeetCode] Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  3. [LeetCode] 394. Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  4. LeetCode 394. 字符串解码(Decode String) 44

    394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...

  5. [Swift]LeetCode394. 字符串解码 | Decode String

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  6. 394. Decode String

    [题目] Total Accepted: 10087 Total Submissions: 25510 Difficulty: Medium Contributors: Admin Given an ...

  7. [LeetCode] Decode String 题解

    题目 题目 s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return " ...

  8. Decode String

    Given an encoded string, return it's decoded string.The encoding rule is: k[encoded_string], where t ...

  9. Leetcode -- 394. Decode String

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

随机推荐

  1. php字符串函数和数组函数

    /验证码$str="abcdefghijklmnopqrstuvwxyz0123456789";$a=substr($str,rand(0,35),1);$b=substr($st ...

  2. Enterprise Library 6——Using the Logging Application Block

    原文参考 http://msdn.microsoft.com/en-us/library/dn440731(v=pandp.60).aspx 一.简介 .更重要的是用于审计.这种日志可以跟踪用户的行为 ...

  3. [搜片神器]使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)

    谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器:http://www.sosobta.com   大家可以给提点意见... 出售商业网站代码,万元起,非诚勿扰,谢谢. 联系h31h31 a ...

  4. win7安装mysql

    转:http://blog.csdn.net/longyuhome/article/details/7913375 Win7系统安装MySQL5.5.21图解 大家都知道MySQL是一款中.小型关系型 ...

  5. 2729:[HNOI2012]排队 - BZOJ

    题目描述 Description某中学有n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个人 ...

  6. Codeforces Beta Round #10 D. LCIS

    题目链接: http://www.codeforces.com/contest/10/problem/D D. LCIS time limit per test:1 secondmemory limi ...

  7. 【HDOJ】【3037】Saving Beans

    排列组合 啊……这题是要求c(n-1,0)+c(n,1)+c(n+1,2)+......+c(n+m-1,m) 这个玩意……其实就等于c(n+m,m) 好吧然后就是模P……Lucas大法好= = 我S ...

  8. httpClient 入门实例

    import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.Unsu ...

  9. 暑假学习日记:Splay树

    从昨天开始我就想学这个伸展树了,今天花了一个上午2个多小时加下午2个多小时,学习了一下伸展树(Splay树),学习的时候主要是看别人博客啦~发现下面这个博客挺不错的http://zakir.is-pr ...

  10. 深入浅出Java并发包—锁(Lock)VS同步(synchronized)

    今天我们来探讨一下Java中的锁机制.前面我们提到,在JDK1.5之前只能通过synchronized关键字来实现同步,这个前面我们已经提到是属于独占锁,性能并不高,因此JDK1.5之后开始借助JNI ...