Skip to content

Commit 15175b5

Browse files
committed
ADD: leetcode solutions
1 parent 00eaa9d commit 15175b5

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

leetcode/AssignCookies.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Arrays;
2+
3+
public class Solution {
4+
public int findContentChildren(int[] g, int[] s) {
5+
Arrays.sort(g);
6+
Arrays.sort(s);
7+
int pg = 0, ps = 0, res = 0;
8+
while (pg < g.length && ps < s.length) {
9+
if (s[ps] >= g[pg]) {
10+
++pg;
11+
++ps;
12+
++res;
13+
} else {
14+
++ps;
15+
}
16+
}
17+
return res;
18+
}
19+
20+
public static void main(String[] args) {
21+
Solution solution = new Solution();
22+
System.out.println(solution.findContentChildren(new int[]{1, 2, 3}, new int[]{1, 1}));
23+
System.out.println(solution.findContentChildren(new int[]{1, 2}, new int[]{1, 2, 3}));
24+
}
25+
}

leetcode/FourSumII.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.HashMap;
2+
3+
public class Solution {
4+
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
5+
int n = A.length;
6+
HashMap<Integer, Integer> cnt = new HashMap<>();
7+
for (int i = 0; i < n; ++i) {
8+
for (int j = 0; j < n; ++j) {
9+
int val = A[i] + B[j];
10+
if (cnt.containsKey(val)) {
11+
cnt.put(val, cnt.get(val) + 1);
12+
} else {
13+
cnt.put(val, 1);
14+
}
15+
}
16+
}
17+
int res = 0;
18+
for (int i = 0; i < n; ++i) {
19+
for (int j = 0; j < n; ++j) {
20+
int val = -(C[i] + D[j]);
21+
if (cnt.containsKey(val)) {
22+
res += cnt.get(val);
23+
}
24+
}
25+
}
26+
return res;
27+
}
28+
29+
public static void main(String[] args) {
30+
Solution solution = new Solution();
31+
System.out.println(solution.fourSumCount(new int[]{1, 2}, new int[]{-2, -1}, new int[]{-1, 2}, new int[]{0, 2}));
32+
}
33+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Arrays;
2+
import java.util.Comparator;
3+
4+
public class Solution {
5+
public int findMinArrowShots(int[][] points) {
6+
Arrays.sort(points, new Comparator<int[]>() {
7+
@Override
8+
public int compare(int[] o1, int[] o2) {
9+
return o1[1] - o2[1];
10+
}
11+
});
12+
int res = 0, pv = 0;
13+
for (int i = 0; i < points.length; ++i) {
14+
if (i == 0 || points[i][0] > pv) {
15+
++res;
16+
pv = points[i][1];
17+
}
18+
}
19+
return res;
20+
}
21+
22+
public static void main(String[] args) {
23+
Solution solution = new Solution();
24+
int[][] points = new int[][]{
25+
{-2147483648,2147483647}
26+
};
27+
System.out.println(solution.findMinArrowShots(points));
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Solution {
2+
public boolean repeatedSubstringPattern(String str) {
3+
for (int i = 1; i < str.length(); ++i) {
4+
if (str.length() % i != 0) {
5+
continue;
6+
}
7+
boolean ok = true;
8+
for (int j = 0; j < str.length(); ++j) {
9+
if (str.charAt(j) != str.charAt(j % i)) {
10+
ok = false;
11+
break;
12+
}
13+
}
14+
if (ok) {
15+
return true;
16+
}
17+
}
18+
return false;
19+
}
20+
21+
public static void main(String[] args) {
22+
Solution solution = new Solution();
23+
System.out.println(solution.repeatedSubstringPattern("abab"));
24+
System.out.println(solution.repeatedSubstringPattern("aba"));
25+
System.out.println(solution.repeatedSubstringPattern("abcabcabcabc"));
26+
}
27+
}

0 commit comments

Comments
 (0)