https://leetcode.com/contest/biweekly-contest-2
A:Sum of Digits in the Minimum Number
给一个数组,求最小数的数字和的奇偶性
import java.util.Arrays;
public class Leetcode1085 {
public int sumOfDigits(int[] A) {
Arrays.sort(A);
int s = 0;
for (char x : (A[0] + "").toCharArray()) {
s += x - '0';
}
return 1 - s % 2;
}
}
给了学生的成绩,求每个学生最高五科成绩的平均分
import java.util.Arrays;
/**
* Created by dezhonger on 2019/6/16
*/
public class Leetcode1086 {
public int[][] highFive(int[][] items) {
Arrays.sort(items, (a, b) -> {
if (a[0] != b[0]) return a[0] - b[0];
return -(a[1] - b[1]);
});
int n = items.length;
int[][] ret = new int[n][];
int p = 0;
for (int i = 0; i < n; ) {
int j = i;
while (j < n && items[i][0] == items[j][0]) j++;
int s = 0;
int num = 0;
for (int k = 0; k < 5 && k < j - i; k++) {
s += items[k + i][1];
num++;
}
ret[p++] = new int[]{items[i][0], s / num};
i = j;
}
return Arrays.copyOf(ret, p);
}
}
求可以组成的字符串数组
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Created by dezhonger on 2019/6/16
*/
public class Leetcode1087 {
public static void main(String[] args) {
// new Leetcode1087().expand("{a,b}c{d,e}f");
new Leetcode1087().expand("{a}{a}{a}");
}
public String[] expand(String S) {
for (int i = 0; i < S.length();) {
if (S.charAt(i) == '{') {
List<String> list = new ArrayList<>();
int st = i + 1;
int j = i + 1;
for (; j < S.length(); j++) {
if (S.charAt(j) =='}')break;
}
String[] str = S.substring(st, j).split(",");
list.addAll(Arrays.asList(str));
r.add(list);
i = j + 1;
} else {
int j = i;
StringBuilder sb = new StringBuilder();
for (; j < S.length(); j++) {
if (S.charAt(j) == '{') break;
sb.append(S.charAt(j));
}
List<String> list = new ArrayList<>();
list.add(sb.toString());
r.add(list);
i = j;
}
}
dfs(0, "", r.size());
Collections.sort(res);
return res.toArray(new String[res.size()]);
}
List<String> res = new ArrayList<>();
List<List<String>> r = new ArrayList<>();
void dfs(int index, String tmp, int len) {
if (index == len) {
res.add(tmp);
return;
}
List<String> strings = r.get(index);
for (int i = 0; i < strings.size(); i++) {
dfs(index + 1, tmp + strings.get(i), len);
}
}
}
求旋转180°后和原数不同的数的个数
数量不多,dfs即可
/**
* Created by dezhonger on 2019/6/16
*/
public class Leetcode1088 {
public static void main(String[] args) {
new Leetcode1088().confusingNumberII(10_0000_0000);
}
int res = 0;
int[] a = new int[]{0, 1, 6, 8, 9};
int N;
boolean check(int x) {
int[] b = new int[]{0, 1, 2, 3, 4, 5, 9, 7, 8, 6};
int t = 0;
int cur = x;
while (x > 0) {
t = t * 10 + b[x % 10];
x /= 10;
}
return !(cur == t);
}
void dfs(int pos, int x) {
if (x > 0 && check(x)) {
res++;
}
int start = (pos == 0 && x == 0) ? 1 : 0;
for (int i = start; i < 5; i++) {
if (1L * x * 10 + a[i] <= N) {
dfs(pos + 1, x * 10 + a[i]);
}
}
}
public int confusingNumberII(int N) {
this.N = N;
dfs(0, 0);
return res;
}
}