Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 置换DFS解的实时复杂性_Java_Recursion_Time Complexity_Depth First Search - Fatal编程技术网

Java 置换DFS解的实时复杂性

Java 置换DFS解的实时复杂性,java,recursion,time-complexity,depth-first-search,Java,Recursion,Time Complexity,Depth First Search,常见的解决方案是DFS递归 class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res = new ArrayList<>(); if (nums == null || nums.length == 0) return res; DFS(

常见的解决方案是DFS递归

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums == null || nums.length == 0)
            return res;

        DFS(0, nums, res);

        return res;
    }

    private void DFS(int index, int[] nums, List<List<Integer>> res) {
        if (index == nums.length) {
            **//covert nums to list and add to res** o(n)
            return;
        }

        for (int i = index; i < nums.length; i++) {
            swap(nums, index, i);
            DFS(index + 1, nums, res);
            swap(nums, index, i);
        }
    }

    private void swap(int[] nums, int left, int right) {
        int tmp = nums[left];
        nums[left] = nums[right];
        nums[right] = tmp;
    }
}
类解决方案{
公共列表排列(int[]nums){
List res=new ArrayList();
if(nums==null | | nums.length==0)
返回res;
DFS(0,nums,res);
返回res;
}
私有void DFS(int索引,int[]nums,列表res){
如果(索引==nums.length){
**//要列出并添加到资源**o(n)的隐蔽NUM
返回;
}
for(int i=索引;i
从数学上我们可以看到总的n!不同的排列方式,很多人说o(n!)是时间复杂性

然而,从代码中我们可以看到递归是T(n)=O(n)(用于加法置换)+T(n-1)+T(n-2)+…+T(2)+T(1),我认为O(n!)忽略了O(n)来增加置换 我不知道如何从这个循环中归纳出一个大O,有人能帮忙吗


非常感谢

你是说O(n)吗?O(n)和O(n)之间存在差异。为什么您认为添加到
res
需要O(n)个时间?如果
res
LinkedList
,它将是O(1),如果它是
ArrayList
,它将是O(1)摊销。因为我们正在向res添加列表。。所以我认为是o(n),你是说o(n)?O(n)和O(n)之间存在差异。为什么您认为添加到
res
需要O(n)个时间?如果
res
LinkedList
,它将是O(1),如果它是
ArrayList
,它将是O(1)摊销。因为我们正在向res添加列表。。所以我认为是o(n)