代码 class Solution: def decodeString(self, s: str) -> str: def dfs(start): repeat_str = repeat_count = '' while start < len(s): if s[start].isnumeric(): repeat_count += s[start] elif s[start] == '[': # 更新指针 start, t_str = dfs(start +...
【代码】 publicString decodeString(String s) {intnum=0; StringBuilder sb=newStringBuilder();for(;i<s.length();i++){if(s.charAt(i)=='['){ i++; String tmp=decodeString(s);//把[后余下字符串重新存入后调用,存在数字、字母两情况for(intk=0;k<num;k++){ sb.append(tmp); } num=0;...
*/stringdecodeString3(string s){intlen = s.size();if(len ==0) {return""; }inti =0;returndecodeHelper(s, i); }stringdecodeHelper(string s,int& i){ string result ="";intlen = s.size();while(i < len && s[i] !=']') {// 非数字而是字符的情况下if(s[i] <'0'|| s[i...
在方括号前的数字表示方括号内容的重复次数(括号内的内容可以是字符串或另一个表达式),请将这个表达式展开成一个字符串。 数字只能出现在“[]”前面。 在线测评地址: LintCode 领扣www.lintcode.com/problem/decode-string/?utm_source=sc-zhihu-zq0528 【样例】 样例1 输入: S = abc3[a] 输出: "abc...
[Leetcode 394]编译解码字符串Decode String 【题目】 将格式为 数[数[字母字母]数[字母]] 的字符串展开 Given an encoded string, return its decoded string. The encoding rule is:k[encoded_string], where theencoded_stringinside the square brackets is being repeated exactlyktimes. Note thatkis ...
class Solution { private int i = -1;//全局变量i,记录字符数组指针位置 public String decodeString(String s) { return dfs(s.toCharArray(), s.length()).toString(); } //递归函数 private StringBuilder dfs(char[] chars, int len) { int num = 0; StringBuilder str = new StringBuilder(); wh...
string decodeString(string s) { int i=0; return decode(s,i); } string decode(string s,int &i){ int n=s.length(); string res=""; while(i<n&&s[i]!=']'){ if(s[i]<'0'||s[i]>'9'){ res+=s[i]; i++; }else{ ...
Python: classSolution:defdecodeString(self,s:str)->str:while'['ins:s=re.sub(r'(\d+)\[([A-Za-z]*)\]',lambdam:int(m.group(1))*m.group(2),s)returns
class Solution { private int i = -1;//全局变量i,记录字符数组指针位置 public String decodeString(String s) { return dfs(s.toCharArray(), s.length()).toString(); } //递归函数 private StringBuilder dfs(char[] chars, int len) { int num = 0; StringBuilder str = new StringBuilder(); wh...
public String decodeString2(String s) { if (s.length() == 0) return ""; StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c >= '0' && c <= '9') { ...