Given a collection of 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], and [3,2,1].

思路:遍历数组,对于该字母,它可选择与它之后的字母交换或者是不交换=>带回溯的递归

class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
result.clear();
dfs(num,);
return result; }
void dfs(vector<int> num, int depth)
{
if(depth == num.size()-)
{
result.push_back(num);
return;
}
dfs(num,depth+);
int temp = num[depth];
for(int i = depth+;i< num.size(); i++)
{
num[depth] = num[i];
num[i] = temp;
dfs(num,depth+);
num[i] = num[depth];
num[depth] = temp;
}
} private:
vector<vector<int> > result;
};

思路II:

当字符串长度为2时 a1a2 a2a1
当字符串长度为3时 a3a1a2 a1a3a2 a1a2a3 a3a2a1 a2a3a1 a2a1a3
比较可以得到 其实就是把a3(多出来的元素)插在长度为2时的两个字符串的任意位置

时间复杂度:三个for循环 O(n3)

class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
int size = nums.size();
int resultSize;
int resultIndex;
vector<vector<int>> result;
vector<int> resultItem(,nums[]);
result.push_back(resultItem);
for(int i = ; i <size; i++){ //nums[i] is the num to insert
resultSize = result.size(); //resultSize in the preceeding insert iterate
for(int j = ; j < resultSize; j++){ //iterate the array to do insertion
result[j].push_back(nums[i]);
resultIndex = j;
for(int k = i-; k >=; k--){ //like insertion sort, adjust forward
result.push_back(result[resultIndex]);
result[result.size()-][k+] = result[resultIndex][k];
result[result.size()-][k] = result[resultIndex][k+];
resultIndex = result.size()-;
}
}
}
return result;
}
};

46. Permutations (Back-Track,Sort)的更多相关文章

  1. LeetCode - 46. Permutations

    46. Permutations Problem's Link -------------------------------------------------------------------- ...

  2. [Leetcode][Python]46: Permutations

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...

  3. 46. Permutations 排列数

    46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations. For ex ...

  4. 刷题46. Permutations

    一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...

  5. [LeetCode] 46. Permutations 全排列

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  6. 46. Permutations

    题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...

  7. 【一天一道LeetCode】#46. Permutations

    一天一道LeetCode系列 (一)题目 Given a collection of distinct numbers, return all possible permutations. For e ...

  8. 31. Next Permutation + 46. Permutations + 47. Permutations II + 60. Permutation Sequence

    ▶ 问题:字典序生成有关的问题. ▶ 31. 由当前序列生成字典序里的下一个序列. ● 初版代码,19 ms class Solution { public: void nextPermutation ...

  9. 【LeetCode】46. Permutations (2 solutions)

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

随机推荐

  1. xml生成javabean(zhuan)

    package com.dom4j; import java.io.File;import java.io.FileWriter;import java.io.IOException;import j ...

  2. 20181009-3 选题 Scrum立会报告+燃尽图 02

    Scrum立会报告+燃尽图(02)选题 此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2191 一.小组介绍 组长:刘莹莹 ...

  3. Android EditText 中hint文字大小以及与输入文字颜色保存一致

    SpannableString 这个就是用来处理android 文本信息 可编辑 可点击 感兴趣的自己去看! /* * Copyright (C) 2006 The Android Open Sour ...

  4. python 库 Numpy 中如何求取向量范数 np.linalg.norm(求范数)(向量的第二范数为传统意义上的向量长度),(如何求取向量的单位向量)

    求取向量二范数,并求取单位向量(行向量计算) import numpy as np x=np.array([[0, 3, 4], [2, 6, 4]]) y=np.linalg.norm(x, axi ...

  5. 配置Yaf

    pecl里面的yaf最新测试版http://pecl.php.net/package/Yaf 安装pcre 要先安装pcre, Debian ubuntu执行 sudo apt-get install ...

  6. LaTeX 之 \label 的运用

    LaTeX 之 \label 的运用 前言 大部分的LaTex教程里面都会提到 \label 的标记功能,而如果入门时就玩耍过WinEdt的同学在工具栏上点击各种环境的时候就会发现\label这个东东 ...

  7. rabbitmq学习(三):rabbitmq之扇形交换机、主题交换机

    前言 上篇我们学习了rabbitmq的作用以及直连交换机的代码实现,这篇我们继续看如何用代码实现扇形交换机和主题交换机 一.扇形交换机 1.生产者 /** * 生产者 */ public class ...

  8. Hive教程之metastore的三种模式

    Hive中metastore(元数据存储)的三种方式: 内嵌Derby方式 Local方式 Remote方式 [一].内嵌Derby方式 这个是Hive默认的启动模式,一般用于单元测试,这种存储方式有 ...

  9. ambassador 学习二 认证

    ambassador 可以在请求路由之前进行认证处理,一般的我们可能会使用第三方的认证服务 基本的环境安装可以参考相关文档 安装&&运行qotm 服务 可以参考官方文档,或者https ...

  10. android adb端口被占用解决方法

    1.输入adb devices命令 C:\Users\Nick>adb devices List of devices attached adb server version (31) does ...