LeetCode Retrospective Topic 46 Fully Ranked Problem Solutions

Question details

Given a sequence without repeated numbers, return all possible permutations.
Example:

Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2 ],
[3,2,1]
]

This question is a relatively simple and classic backtracking question, or an exhaustive list of permutations and combinations. This kind of question can be completed by backtracking (dfs+delete operation). The purpose of writing this article is to record the general writing of backtracking. , The second is to remind yourself that you must pay attention to references in java.

Topic analysis

The general idea for the backtracking problem is:

def backtrack(路径,选择列表)if满足条件:
          添加到最终结果
          return
      for 选择 in 选择列表:
             做选择
             backtrack(路径,选择列表)
             撤销选择

(This is what I think in the LeetCode comment area, I think it’s good, @Teki) It’s
basically this idea to write the code.
The code I wrote here is a bit redundant. In fact, you can directly use the contains method of List without marking the array (mark here, and use this in the future), because usually java is still used a little and it is relatively unfamiliar.
Paste the code:

//有思路了,就是建立一个marked数组,记录标记情况,然后for循环,跳过标记的,然后一直往下搜索就行如
    //可以判断一下数组的和是否等于长度来判断是否完了
    //总体还是很简单的
    public List<List<Integer>> permute(int[] nums) {
    
    
        List<List<Integer>> results = new LinkedList<>();
        if(nums==null||nums.length==0)
            return results;

        for(int i=0;i<nums.length;i++)
        {
    
    
            int[] marked = new int[nums.length];
            backtrack(nums,i,marked,new LinkedList<Integer>(),results,0);
        }

        return results;
    }
    public void backtrack(int[] nums, int index, int[] marked, LinkedList<Integer> result, List<List<Integer>> results,int count){
    
    
        //count用于判断当前是否满了
        if(count==nums.length-1&&marked[index]==0){
    
    
            result.add(nums[index]);
            // System.out.println(result);
            results.add((List<Integer>)result.clone());//这里需要进行一个拷贝,不然里面存的都是一个变量,最后值都为0了
            result.remove(result.size()-1);
            return;
        }

        //判断当前索引是否被遍历过
        if(marked[index]==1)
            return;
        //如果都没有,则说明可以遍历
        //先把自己放进result中去
        result.add(nums[index]);
        marked[index]=1;
        count++;
        //再从头开始遍历
        for(int j=0;j<nums.length;j++)
            backtrack(nums,j,marked,result,results,count);
        //最终把自己删掉
//        System.out.println(nums[index]);
        result.remove(result.size()-1);
        marked[index]=0;
        count--;
    }

Here, in results.add((List<Integer>)result.clone());this line, what I wrote at the beginning results.add(result)was that the final output result was empty (because of the backtracking). After thinking about it for a while, I remembered that this is not a basic type, but a reference type, so it must be done After copying it once, I found that the list cannot be cloned. I checked it for a while and found out that it was only possible to use LinkedList or ArrayList, and then a forced type conversion was needed.

Guess you like

Origin blog.csdn.net/qq_34687559/article/details/109090742