solution.md 4.0 KB
Newer Older
1
# 排列序列
F
fix bug  
feilong 已提交
2

3 4
<p>给出集合 <code>[1,2,3,...,n]</code>,其所有元素共有 <code>n!</code> 种排列。</p><p>按大小顺序列出所有排列情况,并一一标记,当 <code>n = 3</code> 时, 所有排列如下:</p><ol>	<li><code>"123"</code></li>	<li><code>"132"</code></li>	<li><code>"213"</code></li>	<li><code>"231"</code></li>	<li><code>"312"</code></li>	<li><code>"321"</code></li></ol><p>给定 <code>n</code> 和 <code>k</code>,返回第 <code>k</code> 个排列。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3, k = 3<strong><br />输出:</strong>"213"</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 4, k = 9<strong><br />输出:</strong>"2314"</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>n = 3, k = 1<strong><br />输出:</strong>"123"</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>1 <= n <= 9</code></li>	<li><code>1 <= k <= n!</code></li></ul>
<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

8
### before
F
fix bug  
feilong 已提交
9

10 11 12 13 14
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
```cpp
int main()
{
    Solution sol;
    int n = 3;
    int k = 3;
    string res;

    res = sol.getPermutation(n, k);
    cout << res;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
```cpp
class Solution
{
public:
    string getPermutation(int n, int k)
    {
        string res;
        vector<int> candidates;

        vector<int> factorial(n + 1);
        factorial[0] = 1;
        int fact = 1;
        for (int i = 1; i <= n; i++)
        {
            candidates.push_back(i);
            fact *= i;
            factorial[i] = fact;
        }
        k -= 1;
        for (int i = n - 1; i >= 0; i--)
        {
            int index = k / factorial[i];
            k = index * factorial[i];
            res.push_back(candidates[index] + '0');
            candidates.erase(candidates.begin() + index);
        }
        return res;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
64

65
### A
F
fix bug  
feilong 已提交
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
```cpp

class Solution
{
public:
    vector<string> res;
    string getPermutation(int n, int k)
    {
        string track;
        traverse(track, n);
        return res[k - 1];
    }
    void traverse(string &track, int n)
    {

        if (track.size() == n)
        {
            res.push_back(track);
            return;
        }
        for (int i = 1; i <= n; i++)
        {
            char c = i + '0';
            if (find(track.begin(), track.end(), c) != track.end())
                continue;

            track.push_back(c);
            traverse(track, n);
            track.pop_back();
        }
    }
};
```

### B
F
fix bug  
feilong 已提交
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
```cpp
class Solution
{
public:
    string getPermutation(int n, int k)
    {
        string ans;

        vector<bool> st(n + 1);

        for (int i = 1; i <= n; i++)
        {

            int f = 1;

            for (int j = n - i; j >= 1; j--)
                f *= j;

            for (int j = 1; j <= n; j++)
            {

                if (!st[j])
                {

                    if (k <= f)
                    {
                        ans += to_string(j);
                        st[j] = 1;
                        break;
                    }

                    k -= f;
                }
            }
        }
        return ans;
    }
};
```

### C
F
fix bug  
feilong 已提交
144

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
```cpp
class Solution
{
public:
    int th;
    string ans;
    string getPermutation(int n, int k)
    {
        string s;
        vector<bool> vec(9, false);
        this->th = 0;

        backtrack(n, k, s, vec);

        return ans;
    }

    bool backtrack(int n, int k, string &s, vector<bool> &vec)
    {

        if (s.length() == n)
        {
            if (++th == k)
            {
                ans = s;
                return true;
            }
        }

        for (char c = '1'; c <= '1' + n - 1; c++)
        {

            if (vec[c - '1'])
                continue;

            s.push_back(c);
            vec[c - '1'] = true;

            if (backtrack(n, k, s, vec))
                return true;

            s.pop_back();
            vec[c - '1'] = false;
        }
        return false;
    }
};
```