Skip to content

Commit 7838230

Browse files
committed
LeetCode 前400题补充
1 parent eda6de1 commit 7838230

File tree

42 files changed

+1436
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1436
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public int maxProfit(int k, int[] prices) {
3+
if (prices == null || prices.length == 0) {
4+
return 0;
5+
}
6+
7+
if(k >= prices.length) return maxProfit(prices);
8+
9+
int n = prices.length;
10+
int[] l = new int[k + 1];
11+
int[] g = new int[k + 1];
12+
13+
for (int i = 1; i < n; i++) {
14+
int diff = prices[i] - prices[i - 1];
15+
for (int j = k; j >= 1; j--) {
16+
l[j] = Math.max(l[j], g[j - 1]) + diff;
17+
g[j] = Math.max(l[j], g[j]);
18+
}
19+
}
20+
21+
return g[k];
22+
}
23+
24+
public int maxProfit(int[] prices) {
25+
// 只要有利润就卖,就是最优解。
26+
int profit = 0;
27+
for(int i = 1; i < prices.length; i++) {
28+
if(prices[i] > prices[i - 1]) {
29+
profit += prices[i] - prices[i - 1];
30+
}
31+
}
32+
return profit;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public int countPrimes(int n) {
3+
4+
boolean[] b = new boolean[n];
5+
6+
for (int i = 2; i * i < n; i++) {
7+
if (b[i] == false) {
8+
for (int j = i; i * j < n; j++) {
9+
b[i * j] = true;
10+
}
11+
}
12+
}
13+
14+
int c = 0;
15+
for (int i = 2; i < n; i++) {
16+
if (b[i] == false) {
17+
c++;
18+
}
19+
}
20+
21+
return c;
22+
23+
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class Solution {
2+
public boolean isIsomorphic(String s, String t) {
3+
4+
if (s == null || t == null) {
5+
return false;
6+
}
7+
8+
if (s.length() != t.length()) {
9+
return false;
10+
}
11+
12+
Map<Character, Character> map = new HashMap<Character, Character>();
13+
Set<Character> set = new HashSet<Character>();
14+
15+
for (int i = 0; i < s.length(); i++) {
16+
char c1 = s.charAt(i);
17+
char c2 = t.charAt(i);
18+
19+
if (map.containsKey(c1)) {
20+
if (map.get(c1) != c2) {
21+
return false;
22+
}
23+
} else {
24+
if (set.contains(c2)) {
25+
return false;
26+
}
27+
map.put(c1, c2);
28+
set.add(c2);
29+
}
30+
}
31+
32+
return true;
33+
34+
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Solution {
2+
public int[] findOrder(int numCourses, int[][] pre) {
3+
// key 课程id,value 前置课程id
4+
Map<Integer, Set<Integer>> map = new HashMap<>();
5+
for (int i = 0; i < pre.length; i++) {
6+
if (map.containsKey(pre[i][0])) {
7+
map.get(pre[i][0]).add(pre[i][1]);
8+
} else {
9+
Set<Integer> set = new HashSet<>();
10+
set.add(pre[i][1]);
11+
map.put(pre[i][0], set);
12+
}
13+
}
14+
15+
int[] visit = new int[numCourses];
16+
List<Integer> ans = new ArrayList<>();
17+
for (int i = 0; i < numCourses; i++) {
18+
if (!dfs(i, visit, map, ans)) {
19+
return new int[0];
20+
}
21+
}
22+
23+
int[] result = new int[numCourses];
24+
for(int i = 0; i < numCourses; i++){
25+
result[i] = ans.get(i);
26+
}
27+
28+
return result;
29+
}
30+
31+
private boolean dfs(int i, int[] visit, Map<Integer, Set<Integer>> map, List<Integer> ans) {
32+
if (visit[i] == -1) {
33+
return false;
34+
}
35+
36+
if (visit[i] == 1) {
37+
return true;
38+
}
39+
40+
visit[i] = -1;
41+
42+
if (map.containsKey(i)) {
43+
for (int pre : map.get(i)) {
44+
if (!dfs(pre, visit, map,ans)) {
45+
return false;
46+
}
47+
}
48+
}
49+
50+
visit[i] = 1;
51+
ans.add(i);
52+
return true;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
public class Solution {
2+
public static int rob(int[] nums) {
3+
4+
if (nums == null || nums.length == 0) {
5+
return 0;
6+
}
7+
8+
if (nums.length == 1) {
9+
return nums[0];
10+
}
11+
12+
if (nums.length == 2) {
13+
return Math.max(nums[0], nums[1]);
14+
}
15+
16+
return Math.max(help(Arrays.copyOfRange(nums, 0, nums.length - 1)),
17+
help(Arrays.copyOfRange(nums, 1, nums.length)));
18+
}
19+
20+
public static int help(int[] nums) {
21+
22+
if (nums.length == 0) {
23+
return 0;
24+
}
25+
26+
if (nums.length == 1) {
27+
return nums[0];
28+
}
29+
30+
int[] P = new int[nums.length];
31+
32+
P[0] = nums[0];
33+
P[1] = Math.max(nums[0], nums[1]);
34+
35+
for (int i = 2; i < nums.length; i++) {
36+
P[i] = Math.max(nums[i] + P[i - 2], P[i - 1]);
37+
}
38+
39+
return P[nums.length - 1];
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public List<List<Integer>> combinationSum3(int k, int n) {
3+
List<List<Integer>> ans = new ArrayList<>();
4+
robot(1, k, n, ans, new ArrayList<Integer>());
5+
return ans;
6+
}
7+
8+
private void robot(int start, int k, int left, List<List<Integer>> ans, List<Integer> tmp) {
9+
if(k < 0 || left < 0) return;
10+
11+
if(k == 0 && left == 0) {
12+
ans.add(new ArrayList<>(tmp));
13+
return;
14+
}
15+
16+
for(int i = start; i <= 9; i++) {
17+
if(left >= i && k > 0) {
18+
tmp.add(i);
19+
robot(i + 1, k - 1, left - i, ans, tmp);
20+
tmp.remove(tmp.size() - 1);
21+
} else {
22+
return;
23+
}
24+
}
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public boolean containsDuplicate(int[] nums) {
3+
if(nums == null || nums.length == 0) return false;
4+
Set<Integer> set = new HashSet<>();
5+
6+
for(int v : nums) {
7+
if(set.contains(v)) return true;
8+
set.add(v);
9+
}
10+
11+
return false;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public boolean containsNearbyDuplicate(int[] nums, int k) {
3+
if(nums == null || nums.length == 0) return false;
4+
Map<Integer, Integer> map = new HashMap<>();
5+
6+
for(int i = 0; i < nums.length; i++) {
7+
if(map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) {
8+
return true;
9+
} else {
10+
map.put(nums[i], i);
11+
}
12+
}
13+
14+
return false;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class Solution {
2+
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
3+
4+
if (k < 1 || t < 0 || nums == null || nums.length < 2) {
5+
return false;
6+
}
7+
8+
SortedSet<Long> set = new TreeSet<Long>();
9+
10+
for (int j = 0; j < nums.length; j++) {
11+
12+
SortedSet<Long> subSet = set.subSet((long) nums[j] - t,
13+
(long) nums[j] + t + 1);
14+
15+
if (!subSet.isEmpty()) {
16+
return true;
17+
}
18+
19+
if (j >= k) {
20+
set.remove((long) nums[j - k]);
21+
}
22+
23+
set.add((long) nums[j]);
24+
}
25+
26+
return false;
27+
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
public class Solution {
2+
public int maximalSquare(char[][] matrix) {
3+
4+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
5+
return 0;
6+
}
7+
8+
int mx = matrix.length;
9+
int my = matrix[0].length;
10+
11+
int[][] dp = new int[mx][my];
12+
int max = 0;
13+
14+
// 初始化第0行
15+
for (int i = 0; i < my; i++) {
16+
if (matrix[0][i] == '1') {
17+
dp[0][i] = 1;
18+
max = 1;
19+
}
20+
}
21+
22+
// 初始化第0列
23+
for (int i = 1; i < mx; i++) {
24+
if (matrix[i][0] == '1') {
25+
dp[i][0] = 1;
26+
max = 1;
27+
}
28+
}
29+
30+
// dp[x][y]=min(dp[x-1][y],dp[x][y-1],dp[x-1][y-1])+1
31+
for (int x = 1; x < mx; x++) {
32+
for (int y = 1; y < my; y++) {
33+
34+
if (matrix[x][y] == '1') {
35+
dp[x][y] = Math.min(Math.min(dp[x - 1][y], dp[x][y - 1]),
36+
dp[x - 1][y - 1]) + 1;
37+
max = Math.max(max, dp[x][y]);
38+
}
39+
40+
}
41+
}
42+
43+
return max * max;
44+
45+
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int countNodes(TreeNode root) {
12+
if(root == null){
13+
return 0;
14+
}
15+
int left = countLevel(root.left);
16+
int right = countLevel(root.right);
17+
if(left == right){
18+
return countNodes(root.right) + (1<<left);
19+
}else{
20+
return countNodes(root.left) + (1<<right);
21+
}
22+
}
23+
private int countLevel(TreeNode root){
24+
int level = 0;
25+
while(root != null){
26+
level++;
27+
root = root.left;
28+
}
29+
return level;
30+
}
31+
}

0 commit comments

Comments
 (0)