leetcode解码字符串

“(”出现,进入递归。

394. Decode String

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].

Examples:

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

class Solution {
public:
    string decodeString(string s) {
        int  p = 0;
        return Decode(p, s);
    }
    string Decode(int& p, const string& s){
        string re;
        int m =0;
        for(;p < s.size();++p){            
            if(s[p] >='0' && s[p] <= '9'){
                m = m*10 + s[p] - '0';
            }else if(s[p]=='[') {
                string tmp = Decode(++p, s);
                for(;m > 0; --m)
                    re += tmp;
            }else if(s[p]==']'){
                return re;
            }else 
                re.push_back(s[p]);        
        }
        return re;
    }
};

726. Number of Atoms

Given a chemical formula (given as a string), return the count of each atom.

An atomic element always starts with an uppercase character, then zero or more lowercase letters, representing the name.

1 or more digits representing the count of that element may follow if the count is greater than 1. If the count is 1, no digits will follow. For example, H2O and H2O2 are possible, but H1O2 is impossible.

Two formulas concatenated together produce another formula. For example, H2O2He3Mg4 is also a formula.

A formula placed in parentheses, and a count (optionally added) is also a formula. For example, (H2O2) and (H2O2)3 are formulas.

Given a formula, output the count of all elements as a string in the following form: the first name (in sorted order), followed by its count (if that count is more than 1), followed by the second name (in sorted order), followed by its count (if that count is more than 1), and so on.

Example 1:
Input:
formula = “H2O”
Output: “H2O”
Explanation:
The count of elements are {‘H’: 2, ‘O’: 1}.
Example 2:
Input:
formula = “Mg(OH)2”
Output: “H2MgO2”
Explanation:
The count of elements are {‘H’: 2, ‘Mg’: 1, ‘O’: 2}.

class Solution {
public:
    string countOfAtoms(string formula) {
        string re;
        int p=0;
        map<string,int> m=Count(formula,p);
        for(auto mm:m){
            re+=mm.first;
            if(mm.second>1)
                re+=to_string(mm.second);
        }
        return re;
    }
    map<string,int> Count(string s,int& p){
        map<string,int> re;
        while(p<s.size()){
            if(s[p]=='('){
                map<string,int> tmp=Count(s,++p);
                for(auto t:tmp){
                    re[t.first]+=t.second;
                }
            }else if(s[p]==')'){
                p++;
                int n=0;
                while(s[p]>='0'&&s[p]<='9'){
                    n=n*10+s[p++]-'0';
                }
                for(auto it=re.begin();it!=re.end();it++)
                    (*it).second*=n;
                return re;
            }else if(s[p]>='A'&&s[p]<='Z'){
                string t;
                t+=s[p++];
                while(s[p]>='a'&&s[p]<='z')
                    t+=s[p++];
                int n=0;
                while(s[p]>='0'&&s[p]<='9'){
                    n=n*10+s[p++]-'0';
                }
                if(n==0)
                    re[t]+=1;
                else 
                    re[t]+=n;
            }   
        }
        return re;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值