L494
//方法一:回溯法
class Solution {
public static int findTargetSumWays(int[] nums, int target) {
cnt =0;
recurve(nums, 0, 0, target);
return cnt;
}
static int cnt = 0;
public static void recurve(int[] nums, int n, int sum, int target) {
if (n == nums.length) {
if (sum == target) cnt++;
return;
}
recurve(nums, n + 1, sum + nums[n], target);
recurve(nums, n + 1, sum - nums[n], target);
}
}
//方法二:深度优先搜索
406. 根据身高重建队列
方法一:堆排序
public static int[][] reconstructQueue(int[][] people) {
PriorityQueue<Integer[]> heap = sort(people);
LinkedList<Integer[]> result = new LinkedList<>();
//插入排序
for (int i = 0; i < people.length; i++) {
Integer[] x = heap.poll();
result.add(x[1], x);
}
int[][] res = new int[people.length][2];
for (int i = 0; i < people.length; i++) {
Integer[] t = result.get(i);
res[i][0] = t[0];
res[i][1] = t[1];
}
return res;
}
public static PriorityQueue<Integer[]> sort(int[][] people) {
PriorityQueue<Integer[]> heap = new PriorityQueue<>(new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
if (o1 == null || o2 == null) return 0;
if (o1[0].equals(o2[0])) {
return o1[1] - o2[1];
}
return o2[0] - o1[0];
}
});
for (int i = 0; i < people.length; i++) {
heap.add(new Integer[]{people[i][0], people[i][1]});
}
return heap;
}
方法二:
leetcode 完全平方数
//方法1: