[LeetCode]46. Permutations
来源:互联网 发布:安庆2017网络效应答案 编辑:程序博客网 时间:2024/06/07 05:45
Description:
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]]
———————————————————————————————————————————————————Solution:
题意:找出给定数组的所有置换序列。
思路:回溯。
class Solution {public: vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> result; backTracking(result, 0, nums); return result; } void backTracking(vector<vector<int>>& result, int begin, vector<int>& nums) { if (begin >= nums.size()) { result.push_back(nums); return; } // 将nums[begin](头元素)与nums数组中每一个元素做交换 // 从nums.size() - 1(尾元素)开始交换,一直回溯到头 // 回溯前后两次swap将所有可能走一遍,防止跳过没有出现过的情况 for (int i = begin; i < nums.size(); i++) { swap(nums[begin], nums[i]); backTracking(result, begin + 1, nums); swap(nums[begin], nums[i]); } }};
阅读全文
0 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
- matlab 空数组的赋值
- centos7.1 RabbitMQ安装配置
- 并查集及其算法实现
- flume 配置插件
- 信管16数据结构:第三章栈和队列的课前翻转学习任务
- [LeetCode]46. Permutations
- Java IO流 进行文件复制
- 安装vmware-tools出现"what is the location of the "ifconfig"program on your machine?"
- 从零开始前端学习[14]:选择器的优先级
- 图像处理基本算法(汇总)以及实现
- SCUT Training 20170920 Problem D
- Day01 Static 自定义工具类
- 【最小费用流】
- 顺序表应用2:多余元素删除之建表算法