惠济免费网站建设,网站关键词如何优化,如何打开网站网页,设计logo网站侵权吗知乎一、前言
1、题目描述和代码仅供参考#xff0c;如果有问题欢迎指出 2、解题代码采用acm模式#xff08;自己处理输入输出#xff09;#xff0c;不采用核心代码模式#xff08;只编程核心函数#xff09; 3、解题代码采用C语言#xff08;ai一键翻译任意语言#xff…一、前言
1、题目描述和代码仅供参考如果有问题欢迎指出 2、解题代码采用acm模式自己处理输入输出不采用核心代码模式只编程核心函数 3、解题代码采用C语言ai一键翻译任意语言或者cpp转Java等任意语言
二、题目说明
题目 给你一个整数集合 nums 按任意顺序 返回它所有不重复的全排列。 举例 输入nums [1,2,3] 输出[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
三、DFS回溯法递归编程实现
解题思路 定义一个f()函数求可行的全排列解如果到叶子节点时该路径可行就记录否则回溯求解 f(123) 1 f(23) f(23 2 f(3), f(3 3
解题代码
#include iostream
#include vectorusing namespace std;// end参数可以用nums.size()代替但是这里为了方便理解所以加上
void permute(vectorint nums, int start, int end, vectorvectorint result) {if (start end) {result.push_back(nums);//到达叶子节点记录结果return;}for (int i start; i end; i) {swap(nums[start], nums[i]);// 将第i个元素和第start个元素交换位置固定第start个元素permute(nums, start 1, end, result);// 递归调用固定[start1, end]这个区间的元素swap(nums[start], nums[i]); // 回溯恢复原来的数组顺序用于下一次循环}
}int main() {vectorint nums { 1, 2, 3 };vectorvectorint result;permute(nums, 0, nums.size() - 1, result);for (auto v : result) {for (int num : v) {cout num ;}cout endl;}return 0;
}四、有重复元素时的解法STL库函数实现
题目说明 当 nums有重复元素时如果采用回溯法 nums有重复元素就用hash记录被选择的数字如果已经被选择过就跳过这当然可以解决问题不过我们有更优雅的解法那就是STL中的库函数这里写出来是因为面试官可能不让你直接调库函数
解题思路 两个函数next_permutation和prev_permutation分别用于生成下一个排列和上一个排列。
next_permutation函数的工作原理是找到从右到左第一个升序对即nums[i] nums[i1]然后找到这个升序对右边第一个大于它的元素即nums[j] nums[i]交换这两个元素的位置最后将升序对右边的元素反转。这样就得到了下一个排列。
prev_permutation函数的工作原理类似只是它是找升序对然后找到这个升序对左边第一个小于它的元素交换这两个元素的位置最后将升序对左边的元素反转。这样就得到了上一个排列。
在main函数中首先调用prev_permutation函数生成所有的上一个排列然后调用next_permutation函数生成所有的下一个排列。这样就可以得到所有的排列而且由于next_permutation函数会跳过所有重复的排列所以可以避免重复。 原理很详细的一篇文章https://blog.csdn.net/myRealization/article/details/104803834 解题代码
#include iostream
#include vector
#include algorithmusing namespace std;bool next_permutation(vectorint nums) {int i nums.size() - 2;while (i 0 nums[i] nums[i 1]) --i;if (i -1) return false;int j nums.size() - 1;while (nums[j] nums[i]) --j;swap(nums[i], nums[j]);reverse(nums.begin() i 1, nums.end());return true;
}bool prev_permutation(vectorint nums) {int i 0;while (i nums.size() - 1 nums[i] nums[i 1]) i;if (i nums.size() - 1) return false;int j nums.size() - 1;while (nums[j] nums[i]) --j;swap(nums[i], nums[j]);reverse(nums.begin(), nums.begin() i 1);return true;
}int main() {vectorint nums {1, 2, 3};vectorvectorint result;while (prev_permutation(nums)){};//O(n)时间复杂度而sort是nlog n do {result.push_back(nums);} while (next_permutation(nums));for(auto i: result){for(auto j : i)cout j ;cout endl;}return 0;
}如此时间复杂度就是On空间复杂度O1如果这样面试还是过不了的话那就不是你的问题了…
迭代器版本实现
templatetypename Iterator
bool myNextPermutation(Iterator start, Iterator end) { //[start,end)Iterator cur end - 1, pre cur - 1; //pre指向partitionNumber while (cur start *pre *cur) --cur, --pre; //从右到左进行扫描发现第一个违背非递减趋势的数字if (cur start) return false; //整个排列逆序 不存在更大的排列 //从右到左进行扫描发现第一个比partitionNumber大的数for (cur end - 1; *cur *pre; --cur); //cur指向changeNumber std::iter_swap(pre, cur);std::reverse(pre 1, end); //将尾部的逆序变成正序 return true;
}templatetypename Iterator
bool myPrevPermutation(Iterator start, Iterator end) { //[start,end)Iterator cur end - 1, pre cur - 1; //pre指向partitionNumber while (cur start *pre *cur) --cur, --pre; //从右到左进行扫描发现第一个违背非递增趋势的数字if (cur start) return false; //整个排列逆序 不存在更小的排列 //从右到左进行扫描发现第一个比partitionNumber小的数for (cur end - 1; *cur *pre; --cur); //cur指向changeNumber std::iter_swap(pre, cur);std::reverse(pre 1, end); //将尾部的逆序变成正序 return true;
}1 2 3 从小到大排序的是最小的排列从大到小排序是最大的排列。
求f(123)的排列实际上是确定了第1个数以后求len-1个数的排列即 1 f(23),依次类推毫无疑问这会想到dfs
然而还有更好的解法 举例 1 3 2,求增长幅度最小下一个排列 32为逆序不可能减小所以要换11和其右侧第一个大于1的数互换 互换之后此时后半部分是逆序倒序交换数字的右侧以后就是最小的排列
精简步骤就是 next 1、从右到左找升序对的位置 2、右侧找第一个大于升序对的位置 3、交换并倒序升序对右侧
prev 1、从左到右找降序对的位置 2、左侧找第一个小于降序对的位置 3、交换并倒序降序对左侧
为什么不会有重复序列 因为next_permutation()函数可以生成给定序列的下一个字典序排列。它通过在序列中查找第一个违反字典序排列的元素并将其交换到序列末尾来实现这一点。 然后它将序列中剩余的元素重新排序以生成下一个字典序排列。
由于next_permutation()函数是基于字典序排列的因此它不会生成重复的序列。这是因为每个元素都只能出现在其原始位置或者在其后面的某个位置上而 不会出现在其前面的位置上。
举个例子假设我们有一个序列 [1, 2, 3]它的下一个字典序排列是 [1, 3, 2]。如果我们再次调用next_permutation()函数它会找到下一个字典序排列 即 [2, 1, 3]。这个过程会一直持续下去直到我们回到原始序列为止。
因此next_permutation()函数可以确保生成的排列是唯一的不会有重复的序列。 文章转载自: http://www.morning.pzcjq.cn.gov.cn.pzcjq.cn http://www.morning.rdzgm.cn.gov.cn.rdzgm.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.prprj.cn.gov.cn.prprj.cn http://www.morning.bkqdg.cn.gov.cn.bkqdg.cn http://www.morning.grpbt.cn.gov.cn.grpbt.cn http://www.morning.wfhnz.cn.gov.cn.wfhnz.cn http://www.morning.gcszn.cn.gov.cn.gcszn.cn http://www.morning.pxtgf.cn.gov.cn.pxtgf.cn http://www.morning.zrqs.cn.gov.cn.zrqs.cn http://www.morning.hmnhp.cn.gov.cn.hmnhp.cn http://www.morning.zzfqn.cn.gov.cn.zzfqn.cn http://www.morning.kmwsz.cn.gov.cn.kmwsz.cn http://www.morning.dzqr.cn.gov.cn.dzqr.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.qjbxt.cn.gov.cn.qjbxt.cn http://www.morning.tfrmx.cn.gov.cn.tfrmx.cn http://www.morning.smj78.cn.gov.cn.smj78.cn http://www.morning.fjmfq.cn.gov.cn.fjmfq.cn http://www.morning.cpljq.cn.gov.cn.cpljq.cn http://www.morning.bpmfr.cn.gov.cn.bpmfr.cn http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn http://www.morning.xrpjr.cn.gov.cn.xrpjr.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.sqhlx.cn.gov.cn.sqhlx.cn http://www.morning.zqwqy.cn.gov.cn.zqwqy.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.zlgbx.cn.gov.cn.zlgbx.cn http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.rgtp.cn.gov.cn.rgtp.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn http://www.morning.mxbks.cn.gov.cn.mxbks.cn http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.youngbase.cn.gov.cn.youngbase.cn http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.sqhtg.cn.gov.cn.sqhtg.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.supera.com.cn.gov.cn.supera.com.cn http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn http://www.morning.wfysn.cn.gov.cn.wfysn.cn http://www.morning.ghssm.cn.gov.cn.ghssm.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.hjwkq.cn.gov.cn.hjwkq.cn http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn http://www.morning.jcfdk.cn.gov.cn.jcfdk.cn http://www.morning.sgpnz.cn.gov.cn.sgpnz.cn http://www.morning.dbrpl.cn.gov.cn.dbrpl.cn http://www.morning.sgbss.cn.gov.cn.sgbss.cn http://www.morning.jzykw.cn.gov.cn.jzykw.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.gkmwk.cn.gov.cn.gkmwk.cn http://www.morning.zlgbx.cn.gov.cn.zlgbx.cn http://www.morning.dmwck.cn.gov.cn.dmwck.cn http://www.morning.dtnzk.cn.gov.cn.dtnzk.cn http://www.morning.snjpj.cn.gov.cn.snjpj.cn http://www.morning.yhjlg.cn.gov.cn.yhjlg.cn http://www.morning.zylrk.cn.gov.cn.zylrk.cn http://www.morning.crxdn.cn.gov.cn.crxdn.cn http://www.morning.jrqcj.cn.gov.cn.jrqcj.cn http://www.morning.lfttb.cn.gov.cn.lfttb.cn http://www.morning.ftnhr.cn.gov.cn.ftnhr.cn http://www.morning.clccg.cn.gov.cn.clccg.cn http://www.morning.flxqm.cn.gov.cn.flxqm.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.drspc.cn.gov.cn.drspc.cn http://www.morning.xcdph.cn.gov.cn.xcdph.cn http://www.morning.fkfyn.cn.gov.cn.fkfyn.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.snbrs.cn.gov.cn.snbrs.cn http://www.morning.skmpj.cn.gov.cn.skmpj.cn http://www.morning.qsxxl.cn.gov.cn.qsxxl.cn http://www.morning.dqwykj.com.gov.cn.dqwykj.com http://www.morning.kyytt.cn.gov.cn.kyytt.cn