zoukankan      html  css  js  c++  java
  • 394. Decode String. 字符串

    Given an encoded string, return its 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].

    Examples:

    s = "3[a]2[bc]", return "aaabcbc".
    s = "3[a2[c]]", return "accaccacc".
    s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/decode-string

    1.字符串中获取数值

    int getDigits() {
            int ret = 0;
            while (ptr < src.size() && isdigit(src[ptr])) {
                ret = ret * 10 + src[ptr++] - '0';
            }
            return ret;
        }
    

    2.常用字符判断函数
    isdigit(cur) //当前字符是否为数字
    isalpha(cur) //当前字符是否为字母

    class Solution {
    public:
        string src; 
        int ptr = 0;
    
        int getDigits() {
            int ret = 0;
            while (ptr < src.size() && isdigit(src[ptr])) {
                ret = ret * 10 + src[ptr++] - '0';
            }
            return ret;
        }
    
        string getString() {
            if (ptr == src.size() || src[ptr] == ']') {
                return "";
            }
    
            char cur = src[ptr]; 
            int repTime = 1;
            string ret;
    
            if (isdigit(cur)) {
                repTime = getDigits(); 
                ++ptr;
    
                string str = getString(); 
                ++ptr;
                while (repTime--) ret += str; 
            } 
            else 
            if (isalpha(cur)) {
                ret += src[ptr];
                ++ptr;
            }
            
            return ret + getString();
        }
    
        string decodeString(string s) {
            src = s;
            ptr = 0;
            return getString();
        }
    };
    
  • 相关阅读:
    机器学习笔记
    使用pelican创建静态博客
    farbox editor是个好东西
    MamBa项目的插件编写-TikiTorch生成器
    通过rundll32运行C#DLL转储内存
    通过调用Windows本地RPC服务器bypass UAC
    浅谈python反序列化漏洞
    [转载]SQL Server提权系列
    certutil在传输payload中的新奇技巧
    AVIator -- Bypass AV tool
  • 原文地址:https://www.cnblogs.com/xgbt/p/12982686.html
Copyright © 2011-2022 走看看