LeetCode Brushing Notes _46. Arreglo completo

El tema es de LeetCode

46. ​​Arreglo completo

Se puede acceder a otras soluciones o código fuente: tongji4m3

descripción

Dada una secuencia sin números repetidos, devuelve todas las permutaciones posibles.

Ejemplo:

输入: [1,2,3]
输出:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

Ideas

El método tradicional de retroceso es casi el mismo que antes.

Solo para lidiar con el problema de repetición, puede usar la matriz marcada para resolver

Código

/*
    这里没有说是按照数字大小的顺序,只要排序没有重复即可
    而这里的直接按照初始的数字顺利得到全排列 例如:7,6,8,1
    这里第一个是7,6,8,1
    而下面那个是,1,6,7,8
    */
private List<List<Integer>> result = new LinkedList<>();
private List<Integer> temp = new LinkedList<>();
private boolean[] marked;
public List<List<Integer>> permute(int[] nums)
{
    
    
    marked = new boolean[nums.length];
    recursive(nums,0);
    return result;
}

private void recursive(int[] nums,int length)
{
    
    
    if(length==nums.length)
    {
    
    
        result.add(new LinkedList<>(temp));//防止引用问题
        return;//终止循环
    }
    for (int i = 0; i < nums.length; i++)
    {
    
    
        //用一个标记数组更加快
        //            if(!temp.contains(nums[i]))//避免重复的
        //            {
    
    
        //                temp.add(nums[i]);
        //                recursive(nums,length+1);
        //                temp.remove(temp.size()-1);
        //            }

        if(!marked[i])
        {
    
    
            temp.add(nums[i]);
            marked[i] = true;
            recursive(nums,length+1);
            marked[i] = false;
            temp.remove(temp.size()-1);
        }
    }
}

Análisis de complejidad

complejidad del tiempo

Peor complejidad de tiempo: O (N × N!) O (N × N!)O ( N×N ! )

Complejidad espacial

O (N) O (N) O ( N )

Supongo que te gusta

Origin blog.csdn.net/weixin_42249196/article/details/108178916
Recomendado
Clasificación