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 ...
随机推荐
- 微软职位内部推荐-Senior SDE for Win Shell Exp
微软近期Open的职位: Job posting title: Senior Software Development Engineer Location: China, Beijing Divisi ...
- How to tune SharePoint 2010 Server for better performance?
http://social.technet.microsoft.com/wiki/contents/articles/7926.sharepoint-2010-tips-for-dealing-wit ...
- TypeError: Object #<IncomingMessage> has no method 'flash'
JavaScript相关代码: router.post('/reg', function(req, res) { //检验用户两次输入的口令是否一致 if (req.body['password-re ...
- 主成分分析(principal components analysis, PCA)——无监督学习
降维的两种方式: (1)特征选择(feature selection),通过变量选择来缩减维数. (2)特征提取(feature extraction),通过线性或非线性变换(投影)来生成缩减集(复合 ...
- Ubuntu系统下USB转串口的使用
PC系统是Ubuntu12.04,与路由器开发板之间用USB转串口线连接. 一.硬件连接 确认Ubuntu对USB转串口设备的支持. 1.# lsmod | grep usbserial如果有usbs ...
- nodeJS实战
github代码托管地址: https://github.com/Iwillknow/microblog.git 根据<NodeJS开发指南>实例进行实战{{%并且希望一步步自己能够逐步将 ...
- Machine Learning Done Wrong
Machine Learning Done Wrong Statistical modeling is a lot like engineering. In engineering, there ar ...
- uva 10131
DP 先对大象体重排序 然后寻找智力的最长升序子列 输出路径.... #include <iostream> #include <cstring> #include &l ...
- HDU4612 Warm up 边双连通分量&&桥&&树直径
题目的意思很简单,给你一个已经连通的无向图,我们知道,图上不同的边连通分量之间有一定数量的桥,题目要求的就是要你再在这个图上加一条边,使得图的桥数目减到最少. 首先要做的就是找出桥,以及每个点所各自代 ...
- mahout安装配置
1.下载mahout 下载地址:http://mahout.apache.org 我下载的最新版:mahout-distribution-0.9 2.把mahout解压到你想存放的文档,我是放在/Us ...