classSolution{ publicintfindPoisonedDuration(int[] timeSeries, int duration){ int lastTime = 0, ans = 0; for(int i = 1; i < timeSeries.length; i++) { ans += Math.min(timeSeries[i]-timeSeries[i-1], duration); } ans += duration; return ans; } }
classSolution{ public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res = new ArrayList<>(); int len = nums.length; if(len == 0) return res;
boolean[] used = newboolean[len]; List<Integer> path = new ArrayList<>();
dfs(nums, len,0,path, used,res); return res; }
privatevoiddfs(int[] nums, int len, int depth, List<Integer> path, boolean[] used, List<List<Integer>> res){ if (depth == len) { res.add(new ArrayList<>(path)); return; }
classSolution{ public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> res = new ArrayList<>(); int len = nums.length; if(len == 0) return res;
boolean[] used = newboolean[len]; List<Integer> path = new ArrayList<>(); Arrays.sort(nums);
dfs(nums, len,0,path, used,res); return res; }
privatevoiddfs(int[] nums, int len, int depth, List<Integer> path, boolean[] used, List<List<Integer>> res){ if (depth == len) { res.add(new ArrayList<>(path)); return; }
publicclassSolution{ publicintFibonacci(int n){ // if (n == 1 || n == 2) return 1; // int a = 1, b = 1, c = 0; // for (int i = 2; i < n; i++) { // c = a+b; // a = b; // b = c; // } // return c; // 递归 if (n == 1 || n == 2) return1; return Fibonacci(n-1)+Fibonacci(n-2); } }
publicclassSolution{ publicintfindKth(int[] a, int n, int K){ // write code here quickSort(a, 0, n-1); return a[a.length - K]; } publicvoidquickSort(int[] a, int left, int right){ if (left < right) { int point = partition(a, left, right); quickSort(a, left, point-1); quickSort(a, point+1, right); } } publicintpartition(int[] a, int left, int right){ int first = a[left]; while(left < right) { while(left < right && a[right] >= first) { right--; } swap(a, left, right); while(left < right && a[left] <= first) { left++; } swap(a, left, right); } return left; } publicvoidswap(int[] a, int i, int j){ int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } }
import java.util.*; publicclassSolution{ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 将给定数组排序 * @param arr int整型一维数组 待排序的数组 * @return int整型一维数组 */ publicint[] MySort (int[] arr) { quickSort(arr , 0 , arr.length-1); return arr; } publicvoidquickSort(int[] list, int left, int right){ if (left < right) { // 分割数组,找到分割点 int point = partition(list, left, right); // 递归调用,对左子数组进行快速排序 quickSort(list, left, point - 1); // 递归调用,对右子数组进行快速排序 quickSort(list, point + 1, right); } }
/** * 分割数组,找到分割点 */ publicintpartition(int[] list, int left, int right){ // 用数组的第一个元素作为基准数 int first = list[left]; while (left < right) { while (left < right && list[right] >= first) { right--; }
publicclassDFS2{ publicstaticvoidmain(String[] args)throws IOException { BufferedReader buff = new BufferedReader(new InputStreamReader(System.in)); String s = buff.readLine(); int len = Integer.parseInt(s.split(" ")[0]); int target = Integer.parseInt(s.split(" ")[1]);
s = buff.readLine(); int[] nums = newint[len]; for (int i = 0; i < len; i++) { nums[i] = Integer.parseInt(s.split(" ")[i]); }
System.out.println(Arrays.toString(nums));
ArrayList<ArrayList<Integer>> res = new ArrayList<>(); ArrayList<Integer> chain = new ArrayList<>();
dfs(nums, chain, target, res);
System.out.println(res); }
privatestaticvoiddfs(int[] nums, ArrayList<Integer> chain, int target, ArrayList<ArrayList<Integer>> res){ int s = sum(chain); if( s >= target) { if (s == target) { // 引用类型new一个否则后面的值可能改变。 // 排序判断是否重复的 ArrayList<Integer> tmp = new ArrayList<>(chain); Collections.sort(tmp); if (!res.contains(tmp)) { res.add(tmp); } } return; }
for (int num : nums) { chain.add(num); dfs(nums, chain, target, res); chain.remove(chain.size() - 1); } }
privatestaticintsum(ArrayList<Integer> chain){ int sum = 0; for (int num : chain) { sum += num; } return sum; }
public class DFS3 { public static void main(String[] args) throws IOException { BufferedReader buff = new BufferedReader(new InputStreamReader(System.in)); String s = buff.readLine(); int len = Integer.parseInt(s.split(" ")[0]);
s = buff.readLine(); int[] nums = new int[len]; for (int i = 0; i < len; i++) { nums[i] = Integer.parseInt(s.split(" ")[i]); }
// 标记哪个数字没使用 boolean[] used = new boolean[nums.length]; System.out.println(Arrays.toString(nums));
ArrayList<ArrayList<Integer>> res = new ArrayList<>(); ArrayList<Integer> chain = new ArrayList<>(); dfs(nums, used, chain, 0, res);
System.out.println(res); }
private static void dfs(int[] nums, boolean[] used, ArrayList<Integer> chain, int index, ArrayList<ArrayList<Integer>> res) { // 截止条件 if (index == nums.length) { res.add(new ArrayList<>(chain)); return; } // 候选节点 for (int i = 0; i < nums.length; i++) { if(!used[i]) { chain.add(nums[i]); used[i] = true; dfs(nums, used, chain, index + 1, res); chain.remove(chain.size() - 1); used[i] = false; } } } }
publicclassDFS4{ publicstaticvoidmain(String[] args)throws IOException { BufferedReader buff = new BufferedReader(new InputStreamReader(System.in)); String s = buff.readLine(); int len = Integer.parseInt(s.split(" ")[0]);
s = buff.readLine(); int[] nums = newint[len]; for (int i = 0; i < len; i++) { nums[i] = Integer.parseInt(s.split(" ")[i]); }
// 标记哪个数字没使用 boolean[] used = newboolean[nums.length]; System.out.println(Arrays.toString(nums));
ArrayList<ArrayList<Integer>> res = new ArrayList<>(); ArrayList<Integer> chain = new ArrayList<>(); dfs(nums, used, chain, 0, res);
System.out.println(res); }
privatestaticvoiddfs(int[] nums, boolean[] used, ArrayList<Integer> chain, int index, ArrayList<ArrayList<Integer>> res){ // 截止条件 if (index == nums.length) { ArrayList<Integer> tmp = new ArrayList<>(chain); if (!res.contains(tmp)) { res.add(new ArrayList<>(chain)); } return; }
// 候选节点 for (int i = 0; i < nums.length; i++) { if(!used[i]) { chain.add(nums[i]); used[i] = true; dfs(nums, used, chain, index + 1, res); chain.remove(chain.size() - 1); used[i] = false; } } } }