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

Commitf0fa969

Browse files
committed
Modified 2 solutions
1 parentb0e07ae commitf0fa969

File tree

2 files changed

+58
-80
lines changed

2 files changed

+58
-80
lines changed
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
classSolution {
2-
publicintnumEquivDominoPairs(int[][]dominoes) {
3-
Map<String,Integer>map =newHashMap <>();
4-
for (inti =0;i <dominoes.length;i++) {
5-
Stringkey =Math.min(dominoes[i][0],dominoes[i][1]) +"|" +Math.max(dominoes[i][0],dominoes[i][1]);
6-
map.put(key,map.getOrDefault(key,0) +1);
2+
publicintnumEquivDominoPairs(int[][]dominoes) {
3+
intcount =0;
4+
Map<String,Integer>map =newHashMap<>();
5+
for (inti =0;i <dominoes.length;i++) {
6+
StringstraightKey =dominoes[i][0] +"/" +dominoes[i][1];
7+
StringrevKey =dominoes[i][1] +"/" +dominoes[i][0];
8+
if (map.containsKey(straightKey)) {
9+
count +=map.get(straightKey);
10+
}
11+
elseif (!straightKey.equals(revKey)) {
12+
if (map.containsKey(revKey)) {
13+
count +=map.get(revKey);
714
}
8-
9-
intcount =0;
10-
for (intvalue :map.values()) {
11-
count += (value * (value -1)) /2;
12-
}
13-
14-
returncount;
15+
}
16+
map.put(straightKey,map.getOrDefault(straightKey,0) +1);
17+
if (dominoes[i][0] !=dominoes[i][1]) {
18+
map.put(revKey,map.getOrDefault(revKey,0) +1);
19+
}
1520
}
21+
returncount;
22+
}
1623
}

‎Medium/Evaluate Division.java

Lines changed: 39 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,44 @@
11
classSolution {
2-
publicdouble[]calcEquation(List<List<String>>equations,double[]values,List<List<String>>queries) {
3-
double[]ans =newdouble[queries.size()];
4-
5-
Map<String,Set<String>>map =newHashMap<>();
6-
Map<String,Double>resMap =newHashMap<>();
7-
8-
for (inti =0;i <equations.size();i++) {
9-
Stringoperand1 =equations.get(i).get(0);
10-
Stringoperand2 =equations.get(i).get(1);
11-
12-
map.computeIfAbsent(operand1,k ->newHashSet<>()).add(operand2);
13-
map.computeIfAbsent(operand2,k ->newHashSet<>()).add(operand1);
14-
15-
resMap.put(operand1 +"|" +operand2,values[i]);
16-
}
17-
18-
for (inti =0;i <queries.size();i++) {
19-
Stringoperand1 =queries.get(i).get(0);
20-
Stringoperand2 =queries.get(i).get(1);
21-
22-
if (!map.containsKey(operand1) || !map.containsKey(operand2)) {
23-
ans[i] = -1.0;
24-
}
25-
elseif (operand1.equals(operand2)) {
26-
ans[i] =1.0;
27-
}
28-
elseif (map.get(operand1).contains(operand2)) {
29-
ans[i] =getSimpleDivisionVal(resMap,operand1,operand2);
30-
}
31-
elseif (operand2.contains(operand1)) {
32-
ans[i] =getSimpleDivisionVal(resMap,operand2,operand1);
33-
}
34-
else {
35-
ans[i] =dfs(map,resMap,operand1,operand2,1.0,newHashSet<>());
36-
}
37-
}
38-
39-
returnans;
2+
publicdouble[]calcEquation(List<List<String>>equations,double[]values,List<List<String>>queries) {
3+
Map<String,List<String>>map =newHashMap<>();
4+
Map<String,List<Double>>valueMap =newHashMap<>();
5+
for (inti =0;i <equations.size();i++) {
6+
List<String>equation =equations.get(i);
7+
map.computeIfAbsent(equation.get(0),k ->newArrayList<>()).add(equation.get(1));
8+
map.computeIfAbsent(equation.get(1),k ->newArrayList<>()).add(equation.get(0));
9+
valueMap.computeIfAbsent(equation.get(0),k ->newArrayList<>()).add(values[i]);
10+
valueMap.computeIfAbsent(equation.get(1),k ->newArrayList<>()).add(1 /values[i]);
4011
}
41-
42-
privatedoubledfs (Map<String,Set<String>>map,Map<String,Double>resMap,Stringoperand1,Stringoperand2,
43-
doublebase,Set<String>visited) {
44-
if (visited.contains(operand1)) {
45-
return -1.0;
46-
}
47-
if (map.get(operand1).contains(operand2)) {
48-
returnbase *getSimpleDivisionVal(resMap,operand1,operand2);
49-
}
50-
51-
visited.add(operand1);
52-
53-
doubleans = -1.0;
54-
Iterator<String>children =map.get(operand1).iterator();
55-
56-
while (children.hasNext()) {
57-
Stringchild =children.next();
58-
if (!visited.contains(child)) {
59-
ans =Math.max(ans,dfs(map,resMap,child,operand2,base *getSimpleDivisionVal(resMap,operand1,child),visited));
60-
}
61-
}
62-
63-
returnans;
12+
double[]ans =newdouble[queries.size()];
13+
for (inti =0;i <queries.size();i++) {
14+
List<String>query =queries.get(i);
15+
ans[i] =dfs(map,valueMap,query.get(0),query.get(1),newHashSet<>(),1.0);
16+
ans[i] =ans[i] ==0.0 ? -1.0 :ans[i];
6417
}
65-
66-
privatedoublegetSimpleDivisionVal (Map<String,Double>resMap,Stringoperand1,Stringoperand2) {
67-
if (resMap.containsKey(operand1 +"|" +operand2)) {
68-
returnresMap.get(operand1 +"|" +operand2);
69-
}
70-
71-
return1 /resMap.get(operand2 +"|" +operand1);
18+
returnans;
19+
}
20+
21+
privatedoubledfs(Map<String,List<String>>map,Map<String,List<Double>>valueMap,Stringa,Stringb,Set<String>set,doublecurr) {
22+
if (set.contains(a)) {
23+
return0.0;
7224
}
25+
if (!map.containsKey(a)) {
26+
return0.0;
27+
}
28+
if (a.equals(b)) {
29+
returncurr;
30+
}
31+
set.add(a);
32+
List<String>children =map.get(a);
33+
List<Double>valueList =valueMap.get(a);
34+
doubletemp =0.0;
35+
for (inti =0;i <children.size();i++) {
36+
temp =dfs(map,valueMap,children.get(i),b,set,curr *valueList.get(i));
37+
if (temp !=0.0) {
38+
break;
39+
}
40+
}
41+
set.remove(a);
42+
returntemp;
43+
}
7344
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp