LeetCode OJ Permutations
2015-03-21 12:41
627 查看
<p style="line-height: 25.2777786254883px; padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14.4444446563721px; box-sizing: border-box;">Given a collection of numbers, return all possible permutations.</p><p style="line-height: 25.2777786254883px; padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14.4444446563721px; box-sizing: border-box;">For example,<br style="line-height: 25.2777786254883px; box-sizing: border-box;" /><code style="line-height: 23.3333358764648px; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13.3333339691162px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">[1,2,3]</code> have the following permutations:<br style="line-height: 25.2777786254883px; box-sizing: border-box;" /><code style="line-height: 23.3333358764648px; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13.3333339691162px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">[1,2,3]</code>, <code style="line-height: 23.3333358764648px; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13.3333339691162px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">[1,3,2]</code>, <code style="line-height: 23.3333358764648px; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13.3333339691162px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">[2,1,3]</code>, <code style="line-height: 23.3333358764648px; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13.3333339691162px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">[2,3,1]</code>, <code style="line-height: 23.3333358764648px; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13.3333339691162px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">[3,1,2]</code>, and <code style="line-height: 23.3333358764648px; box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13.3333339691162px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; background-color: rgb(249, 242, 244);">[3,2,1]</code>.</p><pre name="code" class="cpp">class Solution { public: vector<vector<int> > permute(vector<int> &num) { vector<int> n = num; sort(n.begin(), n.end()); vector<int> original = n; vector<vector<int> > ans; while (1) { ans.push_back(n); next_permutation(n.begin(), n.end()); if (original == n) break; } return ans; } };
相关文章推荐
- <LeetCode OJ> 46. Permutations
- LeetCode OJ Permutations II
- LeetCode_OJ【46】Permutations
- LeetCode-Permutations && unique && next
- [leetcode]46. Permutations
- <LeetCode OJ>Single Number( I / II / III )【136 / 137 / 260】
- <LeetCode OJ> 189. Rotate Array
- <LeetCode OJ>Remove Element【27】
- <LeetCode OJ>Rotate Array【189】
- leetcode:Permutations (序列所有排列)【面试算法题】
- LeetCode Delete Node in a Linked List 刷题 OJ 算法
- LeetCode Number of 1 Bits 刷题 OJ
- LeetCode Lowest Common Ancestor of a Binary Search Tree 刷题 OJ 算法
- [oj.leetcode] #179 - LargestNumber, 如何在7ms内跑完 221个测试用例
- Leetcode Permutations
- <LeetCode OJ> 62. Unique Paths
- [LeetCode]Permutations
- leetcode[46]Permutations
- LeetCode解题报告—— Permutations & Permutations II & Rotate Image
- [leetcode]Permutations