Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9e6859a

Browse files
committed
20190507
1 parent 15e0467 commit 9e6859a

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

‎code/lc343.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package code;
2+
/*
3+
* 343. Integer Break
4+
* 题意:给定一个正整数,找出一组数字和为该数,且这组数的乘积最大
5+
* 难度:Medium
6+
* 分类:Math, Dynamic Programming
7+
* 思路:能减3的减3,因为3得到的乘积最大
8+
* Tips:
9+
*/
10+
public class lc343 {
11+
public int integerBreak(int n) {
12+
if(n==2) return 1;
13+
if(n==3) return 2;
14+
return helper(n);
15+
}
16+
17+
public int helper(int n){
18+
if(n==2) return 2;
19+
if(n==3) return 3;
20+
if(n==4) return 4;
21+
if(n==5) return 6;
22+
else return helper(n-3)*3;
23+
}
24+
}

‎code/lc685.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package code;
2+
/*
3+
* 685. Redundant Connection II
4+
* 题意:一个图,删掉一个边,使其成为一个根树
5+
* 难度:Hard
6+
* 分类:Tree, Depth-first Search, Union Find, Graph
7+
* 思路:要把问题想清楚
8+
* 判断是否有某个节点父节点有两个, 记为e1, e2
9+
* 再判断是否有环
10+
* 4中情况,分别想清楚返回什么
11+
* 自己没想清楚两种情况的交叉,以为判断完第一步就可直接返回
12+
* 如何判断有环,可以利用并查集的思想
13+
* Tips:https://leetcode.com/problems/redundant-connection-ii/discuss/108045/C%2B%2BJava-Union-Find-with-explanation-O(n)
14+
*/
15+
public class lc685 {
16+
public int[] findRedundantDirectedConnection(int[][] edges) {
17+
int[] can1 = {-1, -1};
18+
int[] can2 = {-1, -1};
19+
int[] parent = new int[edges.length + 1];
20+
for (int i = 0; i < edges.length; i++) {
21+
if (parent[edges[i][1]] == 0) {
22+
parent[edges[i][1]] = edges[i][0];
23+
} else {
24+
can2 = new int[] {edges[i][0], edges[i][1]};
25+
can1 = new int[] {parent[edges[i][1]], edges[i][1]};
26+
edges[i][1] = 0;
27+
}
28+
}
29+
for (int i = 0; i < edges.length; i++) {
30+
parent[i] = i;
31+
}
32+
for (int i = 0; i < edges.length; i++) {
33+
if (edges[i][1] == 0) {
34+
continue;
35+
}
36+
int child = edges[i][1], father = edges[i][0];
37+
if (root(parent, father) == child) { //判断father的父节点是不是child
38+
if (can1[0] == -1) {
39+
return edges[i];
40+
}
41+
return can1;
42+
}
43+
parent[child] = father;
44+
}
45+
return can2;
46+
}
47+
48+
int root(int[] parent, int i) {
49+
while (i != parent[i]) { //找到根为止
50+
parent[i] = parent[parent[i]];
51+
i = parent[i];
52+
}
53+
return i;
54+
}
55+
}

‎readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ LeetCode 指南
190190
| 337 [Java](./code/lc337.java)
191191
| 338 [Java](./code/lc338.java)
192192
| 341 [Java](./code/lc341.java)
193-
| 344 [Java](./code/lc338.java)
193+
| 343 [Java](./code/lc343.java)
194+
| 344 [Java](./code/lc344.java)
194195
| 347 [Java](./code/lc347.java)
195196
| 350 [Java](./code/lc350.java)
196197
| 371 [Java](./code/lc371.java)
@@ -217,6 +218,7 @@ LeetCode 指南
217218
| 617 [Java](./code/lc617.java)
218219
| 621 [Java](./code/lc621.java)
219220
| 647 [Java](./code/lc647.java)
221+
| 685 [Java](./code/lc685.java)
220222
| 714 [Java](./code/lc714.java)
221223
| 746 [Java](./code/lc746.java)
222224
| 771 [Java](./code/lc771.java)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /