LeetCode 394 字符串解码 & 华为2019/4/10机试改编
题目
给定一个经过编码的字符串,返回它解码后的字符串。
编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。
你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。
此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。
示例:
s = “3[a]2[bc]”, 返回 “aaabcbc”.
s = “3[a2[c]]”, 返回 “accaccacc”.
s = “2[abc]3[cd]ef”, 返回 “abcabccdcdcdef”.
华为机试第二题改编
不只有中括号,还有‘{}’,‘[]’,‘()’,输出的字符串逆序展开
示例:
s = “abc3(A)”, 返回 “AAAcba”.
一开始就想到了栈来做,调试了半天,没有处理好几个括号的问题,后来看到牛客,才知道这是leetcode上改编题目,菜是原罪,从现在开始要好好刷题,直到秋招,flag已立下。
解题思路:
1、数据结构选择栈来实现,这是处理括号问题用的比较多的
2、数字可以多个, 要把这些字符的数字转换成整型,表示括号内字母的数目
3、用一个String表示最后的输出
3、分情况进行处理(数字、字母、括号、碰到右括号就开始拼接处理)
代码一 Leetcode394
1 碰到字母,就用String存起来
2 碰到数字,就算出数字字符组成的整型int
3 遇到左边括号"["这种,就把前面的String和int分别压栈
4 遇到右边括号"]"这种,就出栈拼接字符串
import java.util.Stack;
public class LeetCode394 {
public static String decodeString(String s){
char[] str = s.toCharArray();
int i = 0;
Stack<String> stringStack = new Stack<>();
Stack<Integer> countStack = new Stack<>();
int num = 0;
String res = "";
while (i<s.length()){
if (Character.isDigit(str[i])){
while (Character.isDigit(str[i])){
num = num*10 + (str[i]-'0');
i++;
}
}else if(Character.isLetter(str[i])){
res += str[i];
i++;
}else if (str[i] == '['){
stringStack.push(res);
countStack.push(num);
res = "";
num = 0;
i++;
}else if (str[i] == ']'){
int con = countStack.pop();
String temp = res; //保存当前的cd
for (int j=0;j<con-1;j++){
res += temp; // 3[cd] -> cdcdcd
}
res = stringStack.peek() + res;
stringStack.pop();
i++;
}
}
return res;
}
public static void main(String[] args){
String s = "2[abc3[cd]4[e]]";
String out = decodeString(decodeString(s));
String s2 = "2[abc3[c2[a]d]4[e]]";
String out2 = decodeString(decodeString(s2));
System.out.println(out);
System.out.println(out2);
}
}
代码二 华为机试二
1 碰到字母,就用String存起来
2 碰到数字,就算出数字字符组成的整型int
3 遇到左边括号"([{"这种,就把前面的String和int分别压栈
4 遇到右边括号")]}"这种,就出栈拼接字符串
5 最后逆序输出
import java.util.Stack;
public class LeetCode394_2 {
public static String decodeString(String s){
char[] str = s.toCharArray();
int i = 0;
Stack<String> stringStack = new Stack<>();
Stack<Integer> countStack = new Stack<>();
int num = 0;
String res = "";
while (i<s.length()){
if (Character.isDigit(str[i])){
while (Character.isDigit(str[i])){
num = num*10 + (str[i]-'0');
i++;
}
}else if(Character.isLetter(str[i])){
res += str[i];
i++;
}else if (str[i] == '[' || str[i] == '{' || str[i] == '('){
stringStack.push(res);
countStack.push(num);
res = "";
num = 0;
i++;
}else if (str[i] == ']' || str[i] == '}' || str[i] == ')'){
int con = countStack.pop();
String temp = res; //保存当前的cd
for (int j=0;j<con-1;j++){
res += temp; // 3[cd] -> cdcdcd
}
res = stringStack.peek() + res;
stringStack.pop();
i++;
}
}
return res;
}
public static String revise(String s) {
int start = 0;
int end = s.length()-1;
char[] chars = s.toCharArray();
while (start < end){
char temp = chars[start];
chars[start] = chars[end];
chars[end] = temp;
start++;
end--;
}
return new String(chars);
}
public static void main(String[] args){
String s1 = "2{abc3[c2(a)d]4[e]}";
String out1 = decodeString(decodeString(s1));
System.out.println(revise(out1));
String s2 = "abc3(A)";
String out2 = decodeString(decodeString(s2));
System.out.println(revise(out2));
}
}