国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Decode string----字符串解碼問題

2019-11-09 19:47:54
字體:
來源:轉載
供稿:網友

問題描述

Given an encoded string, return it’s decoded string.

The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.

You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.

Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won’t be input like 3a or 2[4].

自己寫的程序:

#include<iostream>#include<string>#include<unordered_map>using namespace std;class Solution {public: string decodeString(string s) { string s2; int k1=0; int k2=0; int k3=0; int i=0; int num; while (i < s.size()) { num = 0; int j = i; while (s[j]!='['&&j<s.size()) { num = num * 10 + s[j] - '0'; j++; } k2 = j+1; while(s[j]!=']'&&j<s.size()) { j++; } k3 = j-1; while(num>0) { for (int i = k2;i <=k3;i++) s2.push_back(s[i]); num--; } i = j+1; } return s2; }};void main(){ Solution s; string a = "3[a]2[b]"; cout << s.decodeString(a) << endl;}

注:只針對沒有嵌套的字符串序列,在寫程序的時候j=i++,和j=i+1有很大的不一樣

參考的程序 class Solution { public: string decodeString(string s, int& i) { string res;

while (i < s.length() && s[i] != ']') { if (!isdigit(s[i])) res += s[i++]; else { int n = 0; while (i < s.length() && isdigit(s[i])) n = n * 10 + s[i++] - '0'; i++; // '[' string t = decodeString(s, i); i++; // ']' while (n-- > 0) res += t; } } return res;}string decodeString(string s) { int i = 0; return decodeString(s, i);}

};


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西乌珠穆沁旗| 聂拉木县| 河源市| 周口市| 北川| 华蓥市| 天峨县| 长子县| 黄浦区| 思茅市| 新巴尔虎右旗| 克拉玛依市| 吴旗县| 屏山县| 利川市| 安化县| 渑池县| 庆安县| 青海省| 麟游县| 资兴市| 石阡县| 云林县| 新化县| 兴安县| 喀什市| 大港区| 正蓝旗| 宿州市| 敖汉旗| 永新县| 金平| 名山县| 芦溪县| 滦平县| 山丹县| 临猗县| 吉水县| 孟州市| 盱眙县| 开阳县|