Skip to content

Commit 7d6f63c

Browse files
committed
ok
1 parent 5285dab commit 7d6f63c

File tree

4 files changed

+133
-12
lines changed

4 files changed

+133
-12
lines changed

src/com/leetcode/ListNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.leetcode;
22

33
public class ListNode {
4-
private int val;
5-
private ListNode next = null;
4+
public int val;
5+
public ListNode next = null;
66
ListNode(int val) {
77
this.val = val;
88
}

src/com/leetcode/Main300.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.leetcode;
22

3+
import java.util.Arrays;
4+
35
/**
46
* 300. 最长上升子序列
57
* 给定一个无序的整数数组,找到其中最长上升子序列的长度。
@@ -8,20 +10,38 @@
810
* 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。
911
*/
1012
public class Main300 {
13+
// public int lengthOfLIS(int[] nums) {
14+
// if (nums==null || nums.length==0)
15+
// return 0;
16+
// int[] dp = new int[nums.length];
17+
// dp[0] = 1;
18+
// int maxAns = 1;
19+
// for (int i = 1; i < dp.length; i++) {
20+
// int curMax = 0;
21+
// for (int j = 0; j < i; j++) {
22+
// if (nums[i] > nums[j])
23+
// curMax = Math.max(curMax, dp[j]);
24+
// }
25+
// dp[i] = curMax + 1;
26+
// maxAns = Math.max(dp[i], maxAns);
27+
// }
28+
// return maxAns;
29+
// }
1130
public int lengthOfLIS(int[] nums) {
12-
if (nums==null || nums.length==0)
13-
return 0;
14-
int[] dp = new int[nums.length];
15-
dp[0] = 1;
31+
int len = nums.length;
32+
if (len < 2) {
33+
return len;
34+
}
35+
int[] dp = new int[len];
36+
Arrays.fill(dp, 1);
1637
int maxAns = 1;
17-
for (int i = 1; i < dp.length; i++) {
18-
int curMax = 0;
38+
for (int i = 1; i < len; i++) {
1939
for (int j = 0; j < i; j++) {
20-
if (nums[i] > nums[j])
21-
curMax = Math.max(curMax, dp[j]);
40+
if (nums[j] < nums[i]) {
41+
dp[i] = Math.max(dp[i], dp[j] + 1);
42+
}
2243
}
23-
dp[i] = curMax + 1;
24-
maxAns = Math.max(dp[i], maxAns);
44+
maxAns = Math.max(maxAns, dp[i]);
2545
}
2646
return maxAns;
2747
}

src/com/leetcode/Main452.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 452. 用最少数量的箭引爆气球
7+
* 在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了。开始坐标总是小于结束坐标。平面内最多存在104个气球。
8+
*
9+
* 一支弓箭可以沿着x轴从不同点完全垂直地射出。在坐标x处射出一支箭,若有一个气球的直径的开始和结束坐标为 xstart,xend, 且满足  xstart ≤ x ≤ xend,则该气球会被引爆。可以射出的弓箭的数量没有限制。 弓箭一旦被射出之后,可以无限地前进。我们想找到使得所有气球全部被引爆,所需的弓箭的最小数量。
10+
*
11+
* Example:
12+
*
13+
* 输入:
14+
* [[10,16], [2,8], [1,6], [7,12]]
15+
*
16+
* 输出:
17+
* 2
18+
*
19+
* 解释:
20+
* 对于该样例,我们可以在x = 6(射爆[2,8],[1,6]两个气球)和 x = 11(射爆另外两个气球)
21+
*/
22+
public class Main452 {
23+
public int findMinArrowShots(int[][] points) {
24+
if (points.length < 1)
25+
return 0;
26+
Arrays.sort(points, (p1, p2) -> p1[1]-p2[1]);
27+
int[] dp = new int[points.length];
28+
Arrays.fill(dp,1);
29+
int res = 1;
30+
for (int i = 1; i < points.length; i++) {
31+
for (int j = 0; j < i; j++) {
32+
if (points[j][1] < points[i][0]) {
33+
dp[i] = Math.max(dp[i], dp[j] + 1);
34+
}
35+
}
36+
res = Math.max(res, dp[i]);
37+
}
38+
return res;
39+
}
40+
41+
public static void main(String[] args) {
42+
Main452 m = new Main452();
43+
int[][] point = new int[][]{{1,2}, {2,3}, {3,4}, {4,5}};
44+
System.out.println(m.findMinArrowShots(point));
45+
}
46+
}

src/com/leetcode/Main46.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
/**
6+
* 46. 全排列
7+
* 给定一个 没有重复 数字的序列,返回其所有可能的全排列。
8+
*
9+
* 示例:
10+
*
11+
* 输入: [1,2,3]
12+
* 输出:
13+
* [
14+
* [1,2,3],
15+
* [1,3,2],
16+
* [2,1,3],
17+
* [2,3,1],
18+
* [3,1,2],
19+
* [3,2,1]
20+
* ]
21+
*/
22+
23+
public class Main46 {
24+
public List<List<Integer>> permute(int[] nums) {
25+
int len = nums.length;
26+
List<List<Integer>> res = new ArrayList<>();
27+
if (len < 1)
28+
return res;
29+
boolean[] used = new boolean[len];
30+
List<Integer> path = new ArrayList<>();
31+
dfs(nums, len, 0, used, path, res);
32+
return res;
33+
}
34+
public void dfs(int[] nums, int len, int depth, boolean[] used, List<Integer> path, List<List<Integer>> res) {
35+
if (depth == len) {
36+
res.add(new ArrayList<>(path));
37+
return;
38+
}
39+
for (int i = 0; i < len; i++) {
40+
if (!used[i]) {
41+
path.add(nums[i]);
42+
used[i] = true;
43+
dfs(nums, len, depth+1, used, path, res);
44+
used[i] = false;
45+
path.remove(path.size()-1);
46+
}
47+
}
48+
}
49+
public static void main(String[] args) {
50+
int[] nums = {1, 2, 3};
51+
Main46 m = new Main46();
52+
List<List<Integer>> lists = m.permute(nums);
53+
System.out.println(lists);
54+
}
55+
}

0 commit comments

Comments
 (0)