[LeetCode][Medium] 1079. Letter Tile Possibilities

Leetcode 網址: https://leetcode.com/problems/letter-tile-possibilities/

題目說明:

給定一個長度 1 到長度 7 的大寫英文字串,
請問用這些字串可以組出不為空的序列有幾組?

解題說明:

這題很顯然是排列組合的問題,
由於題目給定的是一個字串,因此我們需要先把字串轉換成"字母的數量",
接著就直接透遞迴去算排列組合就能得到答案了 ~

程式碼


class Solution {
public:
    int numTilePossibilities(string tiles, int n = 0) {
        int m[26] = {0};
        for (int i = 0; i < tiles.length(); i++) m[tiles[i] - 'A']++;
        return recursive(m, n);
    }
    int recursive(int* m, int& n) {
        for(int i = 0; i < 26; i++) {
            if (!m[i]) continue;
            m[i]--, n++;
            recursive(m, n);
            m[i]++;
        }
        return n;
    }
};

結果

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Letter Tile Possibilities.
Memory Usage: 5.9 MB, less than 100.00% of C++ online submissions for Letter Tile Possibilities.








留言

  1. It's a classy choice designed for superior playability - and it is a stylish addition to any room adorned with traditional, conventional, or transitional decor. As with all of our designs, the Baluster desk 우리카지노 could be custom-made in your choice of bespoke sizes, colors, and finishes. This is the basic rule that each one|that every one} skilled roulette players comply with. Once you assume you could have} found the proper roulette desk in your games - let the roulette wheel spin for a few instances before you place your first guess.

    回覆刪除

張貼留言

這個網誌中的熱門文章

[面試心得] Synology / 群暉 - Product Developr

[面試心得] VICI Holdings / 威旭(高頻交易) - Software Engineer

[面試心得] GoFreight / 聖學科技(Freight Forwarder) - Full-Stack Engineer