前面三题A了,第四题只A了0.8不知道哪错了。。。
第一题:

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Solution solution = new Solution();
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine().replaceAll("\s+", "").trim();
String res = solution.resultHandler(input);
System.out.println(res);
}
}
class Solution {
private final String NEITHER = "Neither";
private HashSet<character> possibleChar = new HashSet<>(
Arrays.asList('a','b','c','d','e','f',
'A','B','C','D','E','F',
'0','1','2','3','4','5','6','7','8','9'));
public String resultHandler(String address) {
if (address.contains(".")) {
return IPV4Handler(address);
} else {
return IPV6Handler(address);
}
}
private String IPV4Handler(String address) {
String[] addressItemList = address.split("\.");
if (addressItemList.length != 4) {
return NEITHER;
}
for (String addressItem : addressItemList) {
if (addressItem.startsWith("0")) {
return NEITHER;
}
int num = Integer.parseInt(addressItem);
if (!(num > 0 && num < 255)) {
return NEITHER;
}
}
return "IPv4";
}
private String IPV6Handler(String address) {
String[] addressItemList = address.split(":");
if (addressItemList.length != 8) {
return NEITHER;
}
for (String addressItem : addressItemList) {
if ("".equals(addressItem)) {
return NEITHER;
}
char[] chars = addressItem.toCharArray();
if (chars.length > 4) {
return NEITHER;
}
for (char c : chars) {
if (!possibleChar.contains(c)) {
return NEITHER;
}
}
}
return "IPv6";
}
}
第二题:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine().replaceAll("\s+", "").trim();
List<string> list = new Solution().resultHandler(input);
System.out.println(list);
}
}
class Solution {
private List<string> res = new ArrayList<>();
private Map<Character, List<character>> map = new HashMap<>();
private String str;
private int length;
{
map.put('2', Arrays.asList('a', 'b', 'c'));
map.put('3', Arrays.asList('d', 'e', 'f'));
map.put('4', Arrays.asList('g', 'h', 'i'));
map.put('5', Arrays.asList('j', 'k', 'l'));
map.put('6', Arrays.asList('m', 'n', 'o'));
map.put('7', Arrays.asList('p', 'q', 'r', 's'));
map.put('8', Arrays.asList('t', 'u', 'v'));
map.put('9', Arrays.asList('w', 'x', 'y', 'z'));
}
public List<string> resultHandler(String str) {
str = str.replace("1", "");
this.str = str;
this.length = str.length();
backTracking(0, new StringBuilder());
return res;
}
private void backTracking(int index, StringBuilder sb) {
if (index == length) {
res.add(sb.toString());
return;
}
List<character> list = map.get(str.charAt(index));
for (Character c : list) {
sb.append(c);
backTracking(index + 1, sb);
sb.deleteCharAt(index);
}
}
}
第三题:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}
int min = new Solution().resultHandler(nums);
System.out.println(min);
}
}
class Solution {
private int min = Integer.MAX_VALUE;
private int[] nums;
private boolean[] used;
private int sum = 0;
public int resultHandler(int[] nums) {
this.nums = nums;
this.used = new boolean[nums.length];
for (int num : nums) {
sum += num;
}
backTracking(0, 0);
return min;
}
private void backTracking(int cur, int count) {
if (count == nums.length) {
return;
}
min = Math.min(min, Math.abs(sum - 2 * cur));
for (int i = 0; i < nums.length; i++) {
if (!used[i]) {
used[i] = true;
backTracking(cur + nums[i], ++count);
used[i] = false;
}
}
}
}
第四题:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if (n == 0) {
System.out.println(0);
}
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}
Arrays.sort(nums);
int max = resultHandler(nums);
System.out.println(max);
}
private static int resultHandler(int[] nums) {
int[][] dp = new int[nums.length][2];
for (int[] item : dp) {
item[0] = 1;
item[1] = -1;
}
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] - nums[j] == dp[j][1]
|| dp[j][1] == -1) {
dp[i][0] = Math.max(dp[i][0], dp[j][0] + 1);
}
}
}
int max = Integer.MIN_VALUE;
for (int[] item : dp) {
max = Math.max(item[0], max);
}
return max;
}
}</character></string></character></string></string></character>