Skip to content

Commit d2e3d7f

Browse files
committed
no message
1 parent ec10c24 commit d2e3d7f

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

18.java

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.Collections;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Set;
8+
import java.util.TreeMap;
9+
10+
class Solution {
11+
public List<List<Integer>> fourSum(int[] nums, int target) {
12+
Set<List<Integer>> res = new HashSet<>();
13+
Arrays.sort(nums);
14+
Map<Integer, List<Bi>> map = new TreeMap<>();
15+
for (int i = 0; i < nums.length - 1; i++) {
16+
for (int j = i + 1; j < nums.length; j++) {
17+
int sum = nums[i] + nums[j];
18+
Bi b = new Bi(i, j);
19+
if (map.containsKey(sum)) {
20+
map.get(sum).add(b);
21+
} else {
22+
List<Bi> a = new ArrayList<>();
23+
a.add(b);
24+
map.put(sum, a);
25+
}
26+
}
27+
}
28+
29+
// go up to half of the target
30+
for (int a : map.keySet()) {
31+
if (a > target / 2) {
32+
break;
33+
}
34+
int b = target - a;
35+
if (map.containsKey(b)) {
36+
List<Bi> indices_a = map.get(a);
37+
List<Bi> indices_b = map.get(b);
38+
for (Bi aa : indices_a) {
39+
for (Bi bb : indices_b) {
40+
if (!aa.con(bb)) {
41+
List<Integer> hit = new ArrayList<>();
42+
hit.add(nums[aa.a]);
43+
hit.add(nums[aa.b]);
44+
hit.add(nums[bb.a]);
45+
hit.add(nums[bb.b]);
46+
Collections.sort(hit);
47+
res.add(hit);
48+
}
49+
}
50+
}
51+
}
52+
}
53+
return new ArrayList<>(res);
54+
}
55+
}
56+
57+
class Bi {
58+
int a;
59+
int b;
60+
61+
Bi(int a, int b) {
62+
this.a = a;
63+
this.b = b;
64+
}
65+
66+
public boolean con(Bi o) {
67+
return this.a == o.a || this.a == o.b || this.b == o.a || this.b == o.b;
68+
}
69+
}

927.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.Arrays;
2+
3+
class Solution927 {
4+
public static void main(String args[]) {
5+
Solution927 s = new Solution927();
6+
int[] arr = new int[] { 1, 0, 1, 0, 1 };
7+
s.threeEqualParts(arr);
8+
}
9+
10+
public int[] threeEqualParts(int[] arr) {
11+
String s = Arrays.toString(arr).replaceAll("\\[|\\]|,|\\s", "");
12+
if (!s.contains("1"))
13+
return new int[] { 0, 2 };
14+
String aa = s.substring(0, 1);
15+
String bb = s.substring(1, s.length() - 1);
16+
String cc = s.substring(s.length() - 1);
17+
String a = trm(aa);
18+
String b = trm(bb);
19+
String c = trm(cc);
20+
while (comp(b, a) >= 0 && comp(b, c) >= 0) {
21+
if (comp(a, b) == 0 && comp(b, c) == 0) {
22+
return new int[] { aa.length() - 1, aa.length() + bb.length() };
23+
}
24+
if (comp(c, a) <= 0) {
25+
cc = bb.charAt(bb.length() - 1) + cc;
26+
bb = bb.substring(0, bb.length() - 1);
27+
} else {
28+
aa = aa + bb.charAt(0);
29+
bb = bb.substring(1);
30+
}
31+
a = trm(aa);
32+
b = trm(bb);
33+
c = trm(cc);
34+
}
35+
36+
return new int[] { -1, -1 };
37+
}
38+
39+
String trm(String s) {
40+
if (s.indexOf("1") >= 0) {
41+
return s.substring(s.indexOf("1"));
42+
} else {
43+
return "0";
44+
}
45+
}
46+
47+
int comp(String s, String o) {
48+
if (s.length() > o.length()) {
49+
return 1;
50+
} else if (o.length() > s.length()) {
51+
return -1;
52+
}
53+
54+
return s.compareTo(o);
55+
}
56+
}

0 commit comments

Comments
 (0)