Movatterモバイル変換


[0]ホーム

URL:


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

Commit87c647d

Browse files
committed
graph
1 parent6ea2b17 commit87c647d

File tree

2 files changed

+86
-12
lines changed

2 files changed

+86
-12
lines changed

‎array/MaxProduct.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@
22

33
publicclassMaxProduct {
44
publicstaticintmaxProduct(int[]A) {
5-
intmax =0;
6-
7-
intlen =A.length;
5+
if (A ==null ||A.length ==0) {
6+
return0;
7+
}
8+
9+
// record the max value in the last node.
10+
intDMax =A[0];
11+
12+
// record the min value in the last node.
13+
intDMin =A[0];
814

9-
intproduct =1;
15+
// This is very important, should recode the A[0] as the initial value.
16+
intmax =A[0];
1017

11-
for (inti =0;i <len;i++) {
12-
if (A[i] <=0) {
13-
max =Math.max(max,product);
14-
product =1;
15-
continue;
16-
}
17-
18-
product *=A[i];
18+
for (inti =1;i <A.length;i++) {
19+
intn1 =DMax *A[i];
20+
intn2 =DMin *A[i];
21+
22+
// we can select the former nodes, or just discade them.
23+
DMax =Math.max(A[i],Math.max(n1,n2));
24+
max =Math.max(max,DMax);
25+
26+
// we can select the former nodes, or just discade them.
27+
DMin =Math.min(A[i],Math.min(n1,n2));
1928
}
2029

2130
returnmax;

‎bfs/CloneGraph.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
packageAlgorithms.bfs;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.HashMap;
5+
importjava.util.LinkedList;
6+
importjava.util.List;
7+
importjava.util.Queue;
8+
9+
publicclassCloneGraph {
10+
classUndirectedGraphNode {
11+
intlabel;
12+
List<UndirectedGraphNode>neighbors;
13+
14+
UndirectedGraphNode(intx) {
15+
label =x;
16+
neighbors =newArrayList<UndirectedGraphNode>();
17+
}
18+
};
19+
20+
publicUndirectedGraphNodecloneGraph(UndirectedGraphNodenode) {
21+
if (node ==null) {
22+
returnnull;
23+
}
24+
25+
HashMap<UndirectedGraphNode,UndirectedGraphNode>map =
26+
newHashMap<UndirectedGraphNode,UndirectedGraphNode>();
27+
28+
Queue<UndirectedGraphNode>q =newLinkedList<UndirectedGraphNode>();
29+
Queue<UndirectedGraphNode>qCopy =newLinkedList<UndirectedGraphNode>();
30+
31+
q.offer(node);
32+
UndirectedGraphNoderootCopy =newUndirectedGraphNode(node.label);
33+
qCopy.offer(rootCopy);
34+
35+
// BFS the graph.
36+
while (!q.isEmpty()) {
37+
UndirectedGraphNodecur =q.poll();
38+
UndirectedGraphNodecurCopy =qCopy.poll();
39+
40+
// bfs all the childern node.
41+
for (UndirectedGraphNodechild :cur.neighbors) {
42+
// the node has already been copied. Just connect it and don't
43+
// need to copy.
44+
if (map.containsKey(child)) {
45+
curCopy.neighbors.add(map.get(child));
46+
continue;
47+
}
48+
49+
// put all the children into the queue.
50+
q.offer(child);
51+
52+
// create a new child and add it to the parent.
53+
UndirectedGraphNodechildCopy =newUndirectedGraphNode(
54+
child.label);
55+
56+
curCopy.neighbors.add(childCopy);
57+
qCopy.offer(childCopy);
58+
59+
map.put(child,childCopy);
60+
}
61+
}
62+
63+
returnrootCopy;
64+
}
65+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp