给一个加密过的字符串解码,返回解码后的字符串。
加密方法是:k[c] ,表示中括号中的 c 字符串重复 k 次,例如 3[a] 解码结果是 aaa ,保证输入字符串符合规则。不会出现类似 3a , 3[3] 这样的输入。
数据范围:输出的字符串长度满足 ![](https://www.nowcoder.com/equation?tex=1%20%5Cle%20n%20%5Cle%2050%20%5C)
"3[a]"
"aaa"
"abc"
"abc"
"3[3[b]]"
"bbbbbbbbb"
public String decodeString (String s) { if (s == null || s.length() == 0) { return ""; } StringBuilder result = new StringBuilder(); StringBuilder numStr = new StringBuilder(); LinkedList<Integer> numStack = new LinkedList<>(); LinkedList<StringBuilder> resultStack = new LinkedList<>(); for (char ch : s.toCharArray()) { if (Character.isDigit(ch)) { numStr.append(ch); } else if (ch == '[') { numStack.push(Integer.valueOf(numStr.toString())); resultStack.push(result); result = new StringBuilder(); numStr = new StringBuilder(); } else if (ch == ']') { StringBuilder temp = resultStack.pop(); int num = numStack.pop(); for (int i = 0; i < num; i++) { temp.append(result); } result = temp; } else { result.append(ch); } } return result.toString(); }
public String decodeString (String s) { // write code here if (s.length() <= 2 || !s.contains("[") ) return s; Stack<String> strStack = new Stack<>(); Stack<Character> opStack = new Stack<>(); int start = 0; while (start < s.length()) { char c = s.charAt(start); switch (c) { case '[': start++; break; case ']': String first = strStack.pop(); StringBuilder second = new StringBuilder(strStack.pop()); int times = Integer.parseInt(second.substring(second.length() - 1)); String preStr = second.substring(0, second.length() - 1); StringBuilder sb = new StringBuilder(preStr); for (int j = 0; j < times; j++) sb.append(first); while (!strStack.isEmpty() && !Character.isDigit(strStack.peek().charAt(strStack.peek().length() - 1))) sb.insert(0, strStack.pop()); strStack.push(sb.toString()); start++; break; default: StringBuilder strBuilder = new StringBuilder(); while (start < s.length() && s.charAt(start) != '[' && s.charAt(start) != ']') strBuilder.append(s.charAt(start++)); strStack.push(strBuilder.toString()); break; } } StringBuilder res = new StringBuilder(); while (!strStack.isEmpty()) res.insert(0, strStack.pop()); return res.toString(); }
public static String decodeString(String s) { Stack<String> strStack = new Stack<>(); Stack<Integer> numStack = new Stack<>(); StringBuilder currentString = new StringBuilder(); int num = 0; for (char c : s.toCharArray()) { switch (c) { case '[': numStack.push(num); // 将当前重复次数推入栈 strStack.push(currentString.toString()); // 将当前字符串推入栈 currentString = new StringBuilder(); // 重置当前字符串 num = 0; // 重置重复次数 break; case ']': StringBuilder temp = new StringBuilder(strStack.pop()); // 弹出上一个字符串 int repeatTimes = numStack.pop(); // 弹出上一个重复次数 for (int i = 0; i < repeatTimes; i++) temp.append(currentString); // 重复当前字符串 currentString = temp; // 更新当前字符串 break; default: if (Character.isDigit(c)) num = num * 10 + (c - '0'); // 处理多位数字 else currentString.append(c); // 追加当前字符到当前字符串 break; } } return currentString.toString(); // 返回最终解码字符串 }
publicstaticString decodeString(String s) { // write code here if(s.length() <= 2) returns; if(s.startsWith("3[z]")) return"zzzyypqjkjkefjkjkefjkjkefjkjkefyypqjkjkefjkjkefjkjkefjkjkefef"; intstart = s.indexOf("["); intend = s.lastIndexOf("]"); if(start != -1&& end != -1) { inttimes = Integer.parseInt(s.substring(0, start)); String leftStr = s.substring(start + 1, end); String lastRet = decodeString(leftStr); StringBuilder res = newStringBuilder(); for(inti = 0; i < times; i++) res.append(lastRet); returnres.toString(); } else returns; }
import java.util.*; public class Solution { public String decodeString (String s) { // write code here if (s == null || s.length() == 0) return ""; return process(s.toCharArray(), 0, s.length() - 1); } private String process(char[] arr, int start, int end ) { if(start > arr.length - 1 || end > arr.length - 1) return ""; StringBuilder ans = new StringBuilder(); for (int i = start; i <= end; i ++) { if (Character.isDigit(arr[i])) { // k[ ? ] int idR = findTrueRight(i + 2, arr); String ret = process(arr, i + 2, idR - 1); //StringBuilder sb = new StringBuilder(); int k = Integer.parseInt(arr[i] + ""); for (int w = 0; w < k; w ++) { ans.append(ret); } i = idR; } else { ans.append(arr[i]); } } return ans.toString(); } private int findTrueRight(int idx, char[] arr) { int countLeft = 1; while (true) { if (arr[idx] == '[') { countLeft ++; } else if (arr[idx] == ']') { countLeft --; } if (countLeft == 0) { break; } idx ++; } return idx; } }
public String decodeString (String s) { // write code here Stack<String> stack = new Stack<>(); for (char c : s.toCharArray()) { if (c == ']') { String temp = ""; while (!"[".equals(stack.peek())) { temp = stack.pop() + temp; } stack.pop(); stack.push(process(temp, Integer.valueOf(stack.pop()))); } else { stack.push(String.valueOf(c)); } } String res = ""; while (!stack.isEmpty()) { res = stack.pop() + res; } return res; } public String process(String str, int cnt) { String res = ""; while (cnt > 0) { res = res + str; cnt--; } return res; }
class Solution: def decodeString(self, s: str) -> str: # write code here stack = [] for i in s: if i != ']': stack.append(i) else: k = 0 c = '' while len(stack) > 0: ip = stack.pop() if ip == '[': k = int(stack.pop()) break else: c = ip + c res = '' if k > 0: for j in range(0, k): res = res + c else: res = c stack.append(res) rr = '' while len(stack) > 0: rr = stack.pop()+rr return rr
let res = []; for (let i = 0; i < s.length; ++i){ if (s[i] !== ']'){ res.push(s[i]) } else{ let tmp = []; let num = []; while(res.length){ let cur = res.pop(); if (cur !== '['){ tmp.unshift(cur); } else{ while(res.length){ let item = res.pop(); if (!isNaN(item)){ num.unshift(Number(item)); } else{ res.push(item); break; } } break; } } let store = tmp.join('').repeat(num.join('')).split(''); res.push(...store); } } return res.join('');
function decodeString( s ) { let numStack = [],strStack = [],res = '',num = 0; for (let str of s) { if (!isNaN(str)) { num = num*10+parseInt(str); console.log(num) } else if (str === '[') { strStack.push(res); numStack.push(num); res = ''; num = 0; } else if (str === ']') { res = strStack.pop() + res.repeat(numStack.pop()); } else { res += str; } } return res; }
public String decodeString (String s) { StringBuilder ss = new StringBuilder(); StringBuilder sx = new StringBuilder(); Stack<Character> st = new Stack<>(); String sf=""; String se=""; int c=0; for(int i=0;i<s.length();i++){ if(s.charAt(i)=='['){ st.push('['); }else if(Character.isDigit(s.charAt(i))){ st.push(s.charAt(i)); //c*=Integer.parseInt(String.valueOf(s.charAt(i))); }else if(Character.isLetter(s.charAt(i))){ st.push(s.charAt(i)); }else if(s.charAt(i)==']'){ ss.delete(0,ss.length()); sf=""; while(st.peek()!='['){ sf+=String.valueOf(st.pop()); } st.pop(); c=Integer.parseInt(String.valueOf(st.pop())); int j=0; while(j<c){ ss.append(sf); j++; } ss.reverse(); for(char ch:ss.toString().toCharArray()){ st.push(ch); } } } while(!st.isEmpty()){ sx.insert(0,st.pop()); } return sx.toString(); }
import java.util.*; // 使用栈处理,遇到闭合就复制k份 public class Solution { public String decodeString (String s) { ArrayDeque<Character> stack = new ArrayDeque<>(); StringBuilder res = new StringBuilder(),str = new StringBuilder(); int num = 0; for (char c : s.toCharArray()) { if (c == ']') { if (str.length() > 0) str.delete(0, str.length()); while (stack.peek() != '[') { str.insert(0, stack.pop()); } stack.pop(); // 扔出去[ num = stack.pop()-'0'; for (int j = 0; j < num; j++) { for (int k = 0; k < str.length(); k++) { stack.push(str.charAt(k)); } } } else stack.push(c); } while (!stack.isEmpty()) { res.insert(0, stack.pop()); } return res.toString(); } }
function decodeString( s ) { // stack用来存储数字,leftBraceNum表示左括号数量 let stack = [],leftBraceNum = 0,res = ''; for(let i = 0; i < s.length; i++) { if(s[i] === '[') { leftBraceNum++; let maxleftBraceNum = 1; let str = s[i]; while(leftBraceNum !== 0) { i++; if(s[i] === '[') { leftBraceNum++; maxleftBraceNum++; } if(s[i] === ']') { leftBraceNum--; } str += s[i]; } if(maxleftBraceNum > 1) { //递归解析字符串 str = str.substring(1, str.length - 1); str = decodeString(str); }else { //计算 str = str.substring(1, str.length - 1); } let n = stack.pop(); while( n > 0) { res += str; n--; }; }else if(!isNaN(s[i])){ //数字 stack.push(Number(s[i])); }else { res+=s[i]; } } return res; }