Leetcode顔質問46:デジタル文字列に変換

タイトル説明

0「」、「B」に翻訳1、......、「L」に翻訳11、...、25が「Z」に翻訳翻訳:デジタル与え、我々は、文字列を変換するには、次の規則に従います。デジタル変換よりもあるかもしれません。多くの異なる方法が翻訳を持っている数を計算する方法に機能をプログラミングしてください。

 

例1:

入力:12258
出力:5
説明:12258 5つの異なる翻訳、すなわち"bccfi"、 "BWFI"、あります "bczi"、 "mcfi" と"MZIは"
 

ヒント:

0 <= NUM​​ <231

出典:滞在ボタン(LeetCode)
リンクします。https://leetcode-cn.com/problems/ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof
すべてのネットワークからの控除が著作権を保有。商業転載は、ソースを明記してください許可公式、非商用の転載をご連絡ください。

 

 

問題解決のためのアイデア

class Solution {
public:
    unordered_set<string> st;
    void dfs(string str,int cur,string &tmp,int &sum){
        if(cur>=str.length()){
            if(tmp!="") ++sum;
            return;
        }
        tmp+=str[cur];
        if(st.find(tmp)!=st.end()){
            dfs(str,cur+1,tmp,sum);
            tmp="";
            dfs(str,cur+1,tmp,sum);
        }
    }
    int translateNum(int num) {
        int ans = 0;
        string tmp="";
        for(int i=0;i<=25;++i) st.insert(to_string(i));
        dfs(to_string(num),0,tmp,ans);
        return ans;
    }
};

 

公開された598元の記事 ウォン称賛10 ビュー30000 +

おすすめ

転載: blog.csdn.net/weixin_35338624/article/details/104468508