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

Commit175f9df

Browse files
committed
1584-min-cost-to-connect-all-points-2.md Perfected code.
1 parent6c22e3a commit175f9df

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

‎en/1001-2000/1584-min-cost-to-connect-all-points-2.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ This page, I will only talk about the solution of **Kruskal's Algorithm**.
7373
```python
7474
classSolution:
7575
def__init__(self):
76-
self.parent= []
76+
self.parents= []
7777

7878
defminCostConnectPoints(self,points: List[List[int]]) ->int:
79-
self.parent=list(range(len(points)))
79+
self.parents=list(range(len(points)))
8080
result=0
8181
edged_points= []
8282

@@ -85,43 +85,49 @@ class Solution:
8585
distance=abs(point[0]- points[j][0])+ \
8686
abs(point[1]- points[j][1])
8787
heapq.heappush(edged_points, (distance, i, j))
88-
88+
8989
while edged_points:
9090
distance, i, j= heapq.heappop(edged_points)
9191

92-
ifself.same_root(i, j):
92+
ifself.is_same_root(i, j):
9393
continue
9494

9595
self.unite(i, j)
9696

9797
result+= distance
98-
98+
9999
return result
100100

101101
defunite(self,x,y):
102102
root_x=self.find_root(x)
103103
root_y=self.find_root(y)
104-
self.parent[root_y]= root_x
104+
105+
self.parents[root_y]= root_x
105106

106107
deffind_root(self,x):
107-
if x==self.parent[x]:
108+
parent=self.parents[x]
109+
110+
if x== parent:
108111
return x
109112

110-
self.parent[x]=self.find_root(self.parent[x])
113+
root=self.find_root(parent)
114+
115+
self.parents[x]= root
111116

112-
returnself.parent[x]
117+
returnroot
113118

114-
defsame_root(self,x,y):
119+
defis_same_root(self,x,y):
115120
returnself.find_root(x)==self.find_root(y)
116121
```
117122

118123
##Java
119124
```java
125+
// Welcome to create a PR to complete the code of this language, thanks!
120126
```
121127

122128
##Python
123129
```python
124-
// Welcome to create aPR to complete the code of this language, thanks!
130+
# Welcome to create a PR to complete the code of this language, thanks!
125131
```
126132

127133
##C++

‎zh/1001-2000/1584-min-cost-to-connect-all-points-2.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ This page, I will only talk about the solution of **Kruskal's Algorithm**.
7373
```python
7474
classSolution:
7575
def__init__(self):
76-
self.parent= []
76+
self.parents= []
7777

7878
defminCostConnectPoints(self,points: List[List[int]]) ->int:
79-
self.parent=list(range(len(points)))
79+
self.parents=list(range(len(points)))
8080
result=0
8181
edged_points= []
8282

@@ -85,43 +85,49 @@ class Solution:
8585
distance=abs(point[0]- points[j][0])+ \
8686
abs(point[1]- points[j][1])
8787
heapq.heappush(edged_points, (distance, i, j))
88-
88+
8989
while edged_points:
9090
distance, i, j= heapq.heappop(edged_points)
9191

92-
ifself.same_root(i, j):
92+
ifself.is_same_root(i, j):
9393
continue
9494

9595
self.unite(i, j)
9696

9797
result+= distance
98-
98+
9999
return result
100100

101101
defunite(self,x,y):
102102
root_x=self.find_root(x)
103103
root_y=self.find_root(y)
104-
self.parent[root_y]= root_x
104+
105+
self.parents[root_y]= root_x
105106

106107
deffind_root(self,x):
107-
if x==self.parent[x]:
108+
parent=self.parents[x]
109+
110+
if x== parent:
108111
return x
109112

110-
self.parent[x]=self.find_root(self.parent[x])
113+
root=self.find_root(parent)
114+
115+
self.parents[x]= root
111116

112-
returnself.parent[x]
117+
returnroot
113118

114-
defsame_root(self,x,y):
119+
defis_same_root(self,x,y):
115120
returnself.find_root(x)==self.find_root(y)
116121
```
117122

118123
##Java
119124
```java
125+
// Welcome to create a PR to complete the code of this language, thanks!
120126
```
121127

122128
##Python
123129
```python
124-
// Welcome to create aPR to complete the code of this language, thanks!
130+
# Welcome to create a PR to complete the code of this language, thanks!
125131
```
126132

127133
##C++

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp