leetcode357

用dfs

class Solution {
public:
    int ans = 0;
    int countNumbersWithUniqueDigits(int n) {
        dfs(0,0,n);
        return ans;
    }
    
    void dfs(int cnt,int len,int n)
    {
        if(len == n)
        {
            ans++;
            return;
        }
        else
        {
            for(int i = 0;i<10;i++)
            {
                int flag = 0;
                int t = cnt;
                while(t)
                {
                    int c = t%10;
                    if(c == i) flag = 1;
                    t/=10;
                }
                if(!flag)
                {
                    dfs(cnt*10+i,len+1,n);
                }
            }
        }
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章