zoukankan      html  css  js  c++  java
  • Leetcode 394. 字符串解码

    地址 https://leetcode-cn.com/problems/decode-string/

    给定一个经过编码的字符串,返回它解码后的字符串。
    
    编码规则为: 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".

    解答 

    分析问题存在的输入类型 简化问题。输入类型有两种

    一种是 数字开头[.....]

    一种是 字母开头

    "[ ...]" 中的内容其实还是以上两种输入类型  所以使用递归函数解决。

    代码如下

    class Solution {
    public:
    
        
        string DeInner(const string& s, int& idx)
    {
        if (isdigit(s[idx])) {
            //得到数字
            int start = idx;
            while (isdigit(s[idx])) {
                idx++;
            }
            int loop = atoi(s.substr(start, idx - start).c_str());
    
            string loopStr = DeInner(s, idx);
            string ret;
            for (int i = 0; i < loop; i++) {
                ret += loopStr;
            }
            return ret;
        }
        else if (isalpha(s[idx]) ) {
            //得到字符串
            int start = idx;
            while (isalpha(s[idx])) {
                idx++;
            }
            string ret = s.substr(start, idx - start);
            while (idx < s.size()) {
                ret  += DeInner(s, idx);
            }
            return ret;
        }
        else if (s[idx] == '[') {
            //计算整个[ ]里的字符串
            int start = idx + 1;
            int end = start;
            int count = 1;
            while (count != 0) {
                end++;
                if (s[end] == '[') count++;
                else if (s[end] == ']') count--;
            }
            string innerStr = s.substr(start, end - start);
            int tmp = 0;
            string ret = DeInner(innerStr, tmp);
            while (tmp < innerStr.size()) {
                ret += DeInner(innerStr, tmp);
            }
            idx = end+1 ;
            return ret;
        }
    
        return "";
    }
    
    string decodeString(string s) {
        int idx = 0; string ans;
        while(idx < s.size()){
            ans += DeInner(s, idx);
        }
        return ans;
    }
        
        
        
    };

    居然还能双100 

    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    关于url路径的定义方式
    script标签的位置
    jQuery中ready与load事件的区别
    Python中cv2库和matplotlib库色彩空间排布不一致
    OpenCV for Python 学习笔记 三
    OpenCV for Python 学习笔记 二
    OpenCV for Python 学习笔记 一
    CentOS7配置opencv for python && eclipse c/c++[更新]
    <转>关于 error LNK2019:无法解析的外部符号 ,该符号在函数**中被引用的思考
    OpenCV load 运行出错 cv::Exception 出错
  • 原文地址:https://www.cnblogs.com/itdef/p/12859979.html
Copyright © 2011-2022 走看看