考试的时候没写出来
完了又想了想,把示例两个答案应该做出来了,分享分享
希望能进面试
import java.util.HashSet; import java.util.LinkedList; /* 排列要求: A[i]>A[i-1]; A[i]+M>=A[i-1]; */ public class PDD排序 { public static void main(String[] args) { System.out.println(quez(new int[]{1,2,2,3,},0)); } private static int quez(int[]nums,int M){ DFS(nums,M,new LinkedList<Integer>(),false); return ans; } private static int ans=0; private static HashSet<Integer>set=new HashSet<>(); private static void DFS(int[]nums, int M, LinkedList<Integer>tool,boolean addEle){ if(tool.size()==nums.length){ for (int i = 0; i < tool.size(); i++) { System.out.print(tool.get(i)+" "); } System.out.println(); ans++; return; } for (int i = 0; i < nums.length; i++) { if(set.contains(i))continue; else{ set.add(i); if(tool.isEmpty()){ tool.add(nums[i]); addEle=true; }else if(nums[i]>tool.getLast() || nums[i]+M>=tool.getLast()){ tool.add(nums[i]); addEle=true; }else{ addEle=false; } DFS(nums,M,tool,addEle); if(addEle)tool.removeLast(); set.remove(i); } } } }
全部评论
(0) 回帖