LeetCode: 46. Permutations
来源:互联网 发布:淘宝男鞋高仿店推荐 编辑:程序博客网 时间:2024/04/29 17:53
LeetCode: 46. Permutations
题目描述
Given a collection of distinct numbers, return all possible permutations.
For example, [1,2,3]
have the following permutations:
[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
题目大意就是让我们把给定数组的所有数字全排列列举出来。
解题思路
直接暴力搜索就 OK。
AC代码
class Solution {public: void permute(vector<vector<int>>& ans, vector<int>& nums, vector<int>& now) { if(now.size() == nums.size()) { ans.push_back(now); return; } for(int i = 0; i < nums.size(); ++i) { if(nums[i] != INT_MAX) { // 用过的数字标记为INT_MAX now.push_back(nums[i]); nums[i] = INT_MAX; permute(ans, nums, now); // 取消标记 nums[i] = now.back(); now.pop_back(); } } } vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> ans; vector<int> now; permute(ans, nums, now); return ans; }};
1 0
- [LeetCode]46.Permutations
- LeetCode 46.Permutations
- 46. Permutations Leetcode Python
- LeetCode --- 46. Permutations
- [Leetcode] 46. Permutations
- [leetcode] 46.Permutations
- LeetCode 46. Permutations DFS
- 46. Permutations LeetCode
- Leetcode 46. Permutations
- leetcode 46. Permutations
- LeetCode *** 46. Permutations
- LeetCode 46. Permutations
- leetcode 46. Permutations
- LeetCode - 46. Permutations
- 【leetcode】46. Permutations
- leetcode 46. Permutations
- [LeetCode] 46. Permutations
- leetcode 46. Permutations
- bzoj1149: [CTSC2007]风玲Mobiles
- 我看见一个妹子很不想说他的
- VTK编译的一些报错
- 循环节长度
- 剑指Offer 38 数字在排序数组中出现的次数
- LeetCode: 46. Permutations
- 停止worker脚本
- POJ 3669-Meteor Shower [bfs] 《挑战程序设计竞赛》2.1
- java OOP 继承
- Help him
- php hello world!
- Java的日期与时间(目录)
- 算法与数据结构-常用排序算法总结1-比较排序
- Android的性能优化