LeetCode OJ Permutations

<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;
    }
};



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值