题解 | #牛牛的字符串排列#
牛牛的字符串排列
https://www.nowcoder.com/practice/5286dafdcdff4c9a9e92c7dc440143db
用c++库函数next_permutation可以很轻松解决本题。
#include <algorithm>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @param k int整型
* @return string字符串vector
*/
vector<string> getPermutation(string s, int k) {
sort(s.begin(), s.end()); // 对字符串进行排序
vector<string> ret; // 存储结果的数组
int num = 0; // 当前排列的序号
do {
num++; // 更新当前排列的序号
if (num <= k) {
ret.push_back(s); // 将当前排列加入结果数组中
}
} while (next_permutation(s.begin(), s.end())); // 生成下一个排列
return ret; // 返回结果数组
}
};
刷题笔记啊 文章被收录于专栏
这是我的刷题笔记。