Decode string----字符串解码问题
来源:互联网 发布:android清空缓存数据 编辑:程序博客网 时间:2024/05/29 18:17
问题描述
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);}
};
0 0
- Decode string----字符串解码问题
- LeetCode-394. Decode String (JAVA)解码字符串
- 394. Decode String (解码字符串)
- Leetcode 394. Decode String 字符串解码 解题报告
- leetcode 394. Decode String 字符串解码 + DFS深度优先遍历求解
- 8.python中字符串的编码和解码问题——decode/encode
- Python编码解码问题 encode decode
- Decode String问题及解法
- 394.leetcode Decode String(medium)[栈 字符串]
- 字符串转码String.getBytes(String decode)和new String()
- Decode Ways 把字符串解码成数字组合@LeetCode
- Python字符串的编码与解码(encode与decode)
- LeetCode 271. Encode and Decode Strings(字符串编解码)
- Python字符串的编码与解码(encode与decode)
- Python字符串的编码与解码(encode与decode)
- Java基础之字符串的编码(Encode)和解码(Decode)
- encodeURIComponent编码 URLDecoder.decode解码乱码的问题
- encodeURIComponent编码 URLDecoder.decode解码乱码的问题
- bootload
- JavaScript创建对象(摘录)
- yum 升级mysql5.1.73到5.5版本
- 读写SD卡上的文件
- 第四十五篇:USB Type-C Spec知识点摘要
- Decode string----字符串解码问题
- FFmpeg学习3:播放音频
- Android Intent 教程
- 数据库快照了解
- [干货]资料链接
- C++和Matlab最常用的编程技能,实现复杂算法的基础
- iOS中的几种定时器
- jquery的each()详细介绍
- Restful API