1. 程式人生 > >leetcode解碼字串

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;
    }
};