問題:
難易度:中
説明:
数Nを与え、次にaeiouの5つの母音を組み合わせて、長さNの文字列を形成します。次に、各元の音は、スプライシングaa ae ai aoauやesplicing ee ei eo euなど、aeiouで並べ替えられた独自の位置または後続の位置の文字とのみ組み合わせることができます。aeiouの順序に従って、各文字のみを追跡できます。によって、彼は同じか、彼の後の母音の位置です。
件名リンク:https://leetcode.com/problems/count-sorted-vowel-strings/
入力範囲:
1 <= n <= 50
ケースを入力してください:
Example 1:
Input: n = 1
Output: 5
Explanation: The 5 sorted strings that consist of vowels only are ["a","e","i","o","u"].
Example 2:
Input: n = 2
Output: 15
Explanation: The 15 sorted strings that consist of vowels only are
["aa","ae","ai","ao","au","ee","ei","eo","eu","ii","io","iu","oo","ou","uu"].
Note that "ea" is not a valid string since 'e' comes after 'a' in the alphabet.
Example 3:
Input: n = 33
Output: 66045
私のコード:
具体的には、最後の文字が数字を決定するための鍵であることがわかります。
n = 1の場合、aeiouの終わりの数:1 1 1 1 1であり、順列の合計は1 + 1 + 1 + 1 + 1 = 5です。
n = 2の場合、aeiouの終わりの数:1 2 3 4 5であり、順列の合計は1 + 2 + 3 + 4 + 5 = 15です。
n = 3の場合、aeiouの終わりの数:1 3 6 10 15、順列の総数は35です(自分で描画して分析すると、aの終わりは5つの順列でなければならず、eは4であることがわかります。など。終了文字を数えるだけです。)
とにかく、50以内でテーブルを叩くだけです。
Java:
class Solution {
private static int[] arr = new int[5], map = new int[50];
static { // 静态代码块打表
Arrays.fill(arr, 1);
for(int i = 1;i < 51;i ++) {
int temp = i - 1, n = i;
for(int j : arr) map[temp] += j;
for(int j = 1;j < 5;j ++) arr[j] += arr[j - 1];
}
}
public int countVowelStrings(int n) {
return map[n - 1];
}
}
C ++:
数が少ないので今回は駆け寄りました
static int arr[5] = {1,1,1,1,1}, cache[50], temp;
static bool flag = true;
class Solution {
public:
int countVowelStrings(int n) {
if (flag) {
build(); flag = false;
}
return cache[n - 1];
}
void build() {
memset(cache, 0, 50);
for (int i = 1; i < 51; i++) {
temp = i - 1;
for (int j = 0; j < 5; j ++) cache[temp] += arr[j];
for (int j = 1; j < 5; j++) arr[j] += arr[j - 1];
}
}
};