add
This commit is contained in:
parent
fdbb317969
commit
80317a8214
38
5.leetcode/src/com/fanxb/common/Q40.java
Normal file
38
5.leetcode/src/com/fanxb/common/Q40.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Q40 {
|
||||||
|
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
|
||||||
|
List<List<Integer>> res = new LinkedList<>();
|
||||||
|
Arrays.sort(candidates);
|
||||||
|
this.deal(res, new LinkedList<>(), 0, candidates, target);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deal(List<List<Integer>> res, List<Integer> cur, int i, int[] sources, int target) {
|
||||||
|
if (target < sources[i]) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (; i < sources.length; i++) {
|
||||||
|
if (sources[i] > target) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
List<Integer> temp = new LinkedList<>(cur);
|
||||||
|
temp.add(sources[i]);
|
||||||
|
if (sources[i] == target) {
|
||||||
|
res.add(temp);
|
||||||
|
} else {
|
||||||
|
deal(res, temp, i + 1, sources, target - sources[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new Q40().combinationSum2(new int[]{10, 1, 2, 7, 6, 1, 5}, 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
18
5.leetcode/src/com/fanxb/common/Q507.java
Normal file
18
5.leetcode/src/com/fanxb/common/Q507.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
public class Q507 {
|
||||||
|
|
||||||
|
public boolean checkPerfectNumber(int num) {
|
||||||
|
if (num == 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int sum = 1;
|
||||||
|
double sqrt = Math.floor(Math.sqrt(num));
|
||||||
|
for (int i = 2; i <= sqrt; i++) {
|
||||||
|
if (num % i == 0) {
|
||||||
|
sum += i + num / i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum == num;
|
||||||
|
}
|
||||||
|
}
|
@ -1,36 +1,33 @@
|
|||||||
package com.fanxb.common;
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
public class Q825 {
|
public class Q825 {
|
||||||
public int numFriendRequests(int[] ages) {
|
public int numFriendRequests(int[] ages) {
|
||||||
int res = 0;
|
//年龄计数
|
||||||
Arrays.sort(ages);
|
int[] ageCount = new int[121];
|
||||||
//二分查找100的分界点
|
for (int age : ages) {
|
||||||
int index100 = search(ages, 100, 0, ages.length - 1);
|
ageCount[age]++;
|
||||||
for (int i = search(ages, 15, 0, index100); i <= index100; i++) {
|
|
||||||
int startIndex = search(ages, Double.valueOf(Math.ceil(0.5 * ages[i] + 7)).intValue(), 0, i);
|
|
||||||
res += i - startIndex;
|
|
||||||
}
|
}
|
||||||
for (int i = index100; i < ages.length; i++) {
|
//计算前缀和
|
||||||
int startIndex = search(ages, Double.valueOf(Math.ceil(0.5 * ages[i] + 7)).intValue(), index100, i);
|
int[] preCount = new int[ageCount.length];
|
||||||
res += i - startIndex;
|
for (int i = 1; i < ageCount.length; i++) {
|
||||||
|
preCount[i] = preCount[i - 1] + ageCount[i];
|
||||||
|
}
|
||||||
|
int res = 0;
|
||||||
|
//从15岁开始遍历
|
||||||
|
for (int i = 15; i < ageCount.length; i++) {
|
||||||
|
if (ageCount[i] == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int startAge = i / 2 + 7;
|
||||||
|
res += ageCount[i] * (preCount[i] - preCount[startAge] - 1);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int search(int[] ages, int target, int initL, int initR) {
|
|
||||||
int l = initL, r = initR, mid = (l + r) / 2;
|
|
||||||
while (l < r) {
|
|
||||||
if (ages[mid] < target) {
|
|
||||||
l = mid + 1;
|
|
||||||
} else {
|
|
||||||
r = mid;
|
|
||||||
}
|
|
||||||
mid = (l + r) / 2;
|
|
||||||
}
|
|
||||||
return l;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println(new Q825().numFriendRequests(new int[]{16, 16}));
|
System.out.println(new Q825().numFriendRequests(new int[]{16, 16}));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user