Skip to content

Commit a2bd594

Browse files
committed
feat: add solutions to lc problem: No.0947.Most Stones Removed with Same
Row or Column
1 parent d67c7e8 commit a2bd594

File tree

8 files changed

+399
-51
lines changed

8 files changed

+399
-51
lines changed

basic/sorting/BubbleSort/README.md

+20-24
Original file line numberDiff line numberDiff line change
@@ -94,56 +94,55 @@ func main() {
9494
```
9595

9696
### **C++**
97-
```c++
97+
98+
```cpp
9899
#include <iostream>
99100
#include <vector>
100101
#include <string>
101102

102103
using namespace std;
103104

104105
/* 简单版本 */
105-
void bubblesort( vector<int> & vec )
106+
void bubblesort(vector<int> &vec)
106107
{
107-
for ( int i = 0; i < vec.size() - 1; i++ )
108+
for (int i = 0; i < vec.size() - 1; i++)
108109
{
109-
for ( int j = 0; j < vec.size() - i - 1; j++ )
110+
for (int j = 0; j < vec.size() - i - 1; j++)
110111
{
111-
if ( vec[j] > vec[j + 1] )
112+
if (vec[j] > vec[j + 1])
112113
{
113-
swap( vec[j], vec[j + 1] );
114+
swap(vec[j], vec[j + 1]);
114115
}
115116
}
116117
}
117118
}
118119

119-
120120
/* 改进版本 */
121-
void bubblesort1( vector<int> & vec )
121+
void bubblesort1(vector<int> &vec)
122122
{
123-
for ( int i = 0; i < vec.size() - 1; i++ )
123+
for (int i = 0; i < vec.size() - 1; i++)
124124
{
125125
bool exchange = false;
126-
for ( int j = 0; j < vec.size() - i - 1; j++ )
126+
for (int j = 0; j < vec.size() - i - 1; j++)
127127
{
128-
if ( vec[j] > vec[j + 1] )
128+
if (vec[j] > vec[j + 1])
129129
{
130-
swap( vec[j], vec[j + 1] );
130+
swap(vec[j], vec[j + 1]);
131131
exchange = true;
132132
}
133133
}
134134

135-
if ( !exchange )
135+
if (!exchange)
136136
{
137137
break;
138138
}
139139
}
140140
}
141141

142-
143-
void printvec( const vector<int> & vec, const string & strbegin = "", const string & strend = "" )
142+
void printvec(const vector<int> &vec, const string &strbegin = "", const string &strend = "")
144143
{
145144
cout << strbegin << endl;
146-
for ( auto val : vec )
145+
for (auto val : vec)
147146
{
148147
cout << val << "\t";
149148
}
@@ -152,18 +151,15 @@ void printvec( const vector<int> & vec, const string & strbegin = "", const stri
152151
cout << strend << endl;
153152
}
154153

155-
156-
int main( void )
154+
int main(void)
157155
{
158-
vector<int> vec = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
159-
printvec( vec );
156+
vector<int> vec = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
157+
printvec(vec);
160158

161-
bubblesort1( vec );
159+
bubblesort1(vec);
162160

163-
printvec( vec, "after sort", "" );
161+
printvec(vec, "after sort", "");
164162
}
165-
166-
167163
```
168164
169165
## 算法分析

basic/sorting/BubbleSort/bubblesort.cpp

+18-23
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,46 @@
55
using namespace std;
66

77
/* 简单版本 */
8-
void bubblesort( vector<int> & vec )
8+
void bubblesort(vector<int> &vec)
99
{
10-
for ( int i = 0; i < vec.size() - 1; i++ )
10+
for (int i = 0; i < vec.size() - 1; i++)
1111
{
12-
for ( int j = 0; j < vec.size() - i - 1; j++ )
12+
for (int j = 0; j < vec.size() - i - 1; j++)
1313
{
14-
if ( vec[j] > vec[j + 1] )
14+
if (vec[j] > vec[j + 1])
1515
{
16-
swap( vec[j], vec[j + 1] );
16+
swap(vec[j], vec[j + 1]);
1717
}
1818
}
1919
}
2020
}
2121

22-
2322
/* 改进版本 */
24-
void bubblesort1( vector<int> & vec )
23+
void bubblesort1(vector<int> &vec)
2524
{
26-
for ( int i = 0; i < vec.size() - 1; i++ )
25+
for (int i = 0; i < vec.size() - 1; i++)
2726
{
2827
bool exchange = false;
29-
for ( int j = 0; j < vec.size() - i - 1; j++ )
28+
for (int j = 0; j < vec.size() - i - 1; j++)
3029
{
31-
if ( vec[j] > vec[j + 1] )
30+
if (vec[j] > vec[j + 1])
3231
{
33-
swap( vec[j], vec[j + 1] );
32+
swap(vec[j], vec[j + 1]);
3433
exchange = true;
3534
}
3635
}
3736

38-
if ( !exchange )
37+
if (!exchange)
3938
{
4039
break;
4140
}
4241
}
4342
}
4443

45-
46-
void printvec( const vector<int> & vec, const string & strbegin = "", const string & strend = "" )
44+
void printvec(const vector<int> &vec, const string &strbegin = "", const string &strend = "")
4745
{
4846
cout << strbegin << endl;
49-
for ( auto val : vec )
47+
for (auto val : vec)
5048
{
5149
cout << val << "\t";
5250
}
@@ -55,15 +53,12 @@ void printvec( const vector<int> & vec, const string & strbegin = "", const stri
5553
cout << strend << endl;
5654
}
5755

58-
59-
int main( void )
56+
int main(void)
6057
{
61-
vector<int> vec = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
62-
printvec( vec );
58+
vector<int> vec = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
59+
printvec(vec);
6360

64-
bubblesort1( vec );
61+
bubblesort1(vec);
6562

66-
printvec( vec, "after sort", "" );
63+
printvec(vec, "after sort", "");
6764
}
68-
69-

solution/0900-0999/0947.Most Stones Removed with Same Row or Column/README.md

+164-2
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,189 @@
5555
<li>不会有两块石头放在同一个坐标点上</li>
5656
</ul>
5757

58-
5958
## 解法
6059

6160
<!-- 这里可写通用的实现逻辑 -->
6261

62+
并查集。
63+
64+
并查集模板:
65+
66+
模板 1——朴素并查集:
67+
68+
```python
69+
# 初始化,p存储每个点的父节点
70+
p = list(range(n))
71+
72+
# 返回x的祖宗节点
73+
def find(x):
74+
if p[x] != x:
75+
# 路径压缩
76+
p[x] = find(p[x])
77+
return p[x]
78+
79+
# 合并a和b所在的两个集合
80+
p[find(a)] = find(b)
81+
```
82+
83+
模板 2——维护 size 的并查集:
84+
85+
```python
86+
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
87+
p = list(range(n))
88+
size = [1] * n
89+
90+
# 返回x的祖宗节点
91+
def find(x):
92+
if p[x] != x:
93+
# 路径压缩
94+
p[x] = find(p[x])
95+
return p[x]
96+
97+
# 合并a和b所在的两个集合
98+
if find(a) != find(b):
99+
size[find(b)] += size[find(a)]
100+
p[find(a)] = find(b)
101+
```
102+
103+
模板 3——维护到祖宗节点距离的并查集:
104+
105+
```python
106+
# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离
107+
p = list(range(n))
108+
d = [0] * n
109+
110+
# 返回x的祖宗节点
111+
def find(x):
112+
if p[x] != x:
113+
t = find(p[x])
114+
d[x] += d[p[x]]
115+
p[x] = t
116+
return p[x]
117+
118+
# 合并a和b所在的两个集合
119+
p[find(a)] = find(b)
120+
d[find(a)] = distance
121+
```
122+
63123
<!-- tabs:start -->
64124

65125
### **Python3**
66126

67127
<!-- 这里可写当前语言的特殊实现逻辑 -->
68128

69129
```python
70-
130+
class Solution:
131+
def removeStones(self, stones: List[List[int]]) -> int:
132+
n = 10010
133+
p = list(range(n << 1))
134+
135+
def find(x):
136+
if p[x] != x:
137+
p[x] = find(p[x])
138+
return p[x]
139+
140+
for x, y in stones:
141+
p[find(x)] = find(y + n)
142+
143+
s = set()
144+
for x, _ in stones:
145+
s.add(find(x))
146+
return len(stones) - len(s)
71147
```
72148

73149
### **Java**
74150

75151
<!-- 这里可写当前语言的特殊实现逻辑 -->
76152

77153
```java
154+
class Solution {
155+
private int[] p;
156+
157+
public int removeStones(int[][] stones) {
158+
int n = 10010;
159+
p = new int[n << 1];
160+
for (int i = 0; i < p.length; ++i) {
161+
p[i] = i;
162+
}
163+
for (int[] e : stones) {
164+
p[find(e[0])] = find(e[1] + n);
165+
}
166+
Set<Integer> s = new HashSet<>();
167+
for (int[] e : stones) {
168+
s.add(find(e[0]));
169+
}
170+
return stones.length - s.size();
171+
}
172+
173+
private int find(int x) {
174+
if (p[x] != x) {
175+
p[x] = find(p[x]);
176+
}
177+
return p[x];
178+
}
179+
}
180+
```
181+
182+
### **C++**
183+
184+
```cpp
185+
class Solution {
186+
public:
187+
vector<int> p;
188+
189+
int removeStones(vector<vector<int>> &stones) {
190+
int n = 10010;
191+
p.resize(n << 1);
192+
for (int i = 0; i < p.size(); ++i)
193+
p[i] = i;
194+
for (auto e : stones)
195+
{
196+
p[find(e[0])] = find(e[1] + 10010);
197+
}
198+
unordered_set<int> s;
199+
for (auto e : stones)
200+
{
201+
s.insert(find(e[0]));
202+
}
203+
return stones.size() - s.size();
204+
}
205+
206+
int find(int x) {
207+
if (p[x] != x)
208+
p[x] = find(p[x]);
209+
return p[x];
210+
}
211+
};
212+
```
78213

214+
### **Go**
215+
216+
```go
217+
var p []int
218+
219+
func removeStones(stones [][]int) int {
220+
n := 10010
221+
p = make([]int, n<<1)
222+
for i := 0; i < len(p); i++ {
223+
p[i] = i
224+
}
225+
for _, e := range stones {
226+
p[find(e[0])] = find(e[1] + n)
227+
}
228+
s := make(map[int]bool)
229+
for _, e := range stones {
230+
s[find(e[0])] = true
231+
}
232+
return len(stones) - len(s)
233+
}
234+
235+
func find(x int) int {
236+
if p[x] != x {
237+
p[x] = find(p[x])
238+
}
239+
return p[x]
240+
}
79241
```
80242

81243
### **...**

0 commit comments

Comments
 (0)