Skip to content

Commit 9d6aa6b

Browse files
committed
Moved cpp private code after public code.
1 parent c24d3fe commit 9d6aa6b

6 files changed

Lines changed: 79 additions & 79 deletions

solutions/1-1000/200-number-of-islands-2.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,24 @@ class Solution {
183183
# C++
184184
```cpp
185185
class Solution {
186+
public:
187+
int numIslands(vector<vector<char>>& grid) {
188+
grid_ = grid;
189+
auto island_count = 0;
190+
191+
for (auto i = 0; i < grid_.size(); i++) {
192+
for (auto j = 0; j < grid_[0].size(); j++) {
193+
if (grid_[i][j] == '1') {
194+
island_count++;
195+
196+
depth_first_search(i, j);
197+
}
198+
}
199+
}
200+
201+
return island_count;
202+
}
203+
186204
private:
187205
vector<vector<char>> grid_;
188206
stack<pair<int, int>> vertex_stack;
@@ -217,24 +235,6 @@ private:
217235
vertex_stack.push({i - 1, j});
218236
}
219237
}
220-
221-
public:
222-
int numIslands(vector<vector<char>>& grid) {
223-
grid_ = grid;
224-
auto island_count = 0;
225-
226-
for (auto i = 0; i < grid_.size(); i++) {
227-
for (auto j = 0; j < grid_[0].size(); j++) {
228-
if (grid_[i][j] == '1') {
229-
island_count++;
230-
231-
depth_first_search(i, j);
232-
}
233-
}
234-
}
235-
236-
return island_count;
237-
}
238238
};
239239
```
240240

solutions/1-1000/200-number-of-islands.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -162,24 +162,6 @@ class Solution {
162162
# C++
163163
```cpp
164164
class Solution {
165-
private:
166-
vector<vector<char>> grid_;
167-
168-
void depth_first_search(int i, int j) {
169-
if (i < 0 || i >= grid_.size() ||
170-
j < 0 || j >= grid_[0].size() ||
171-
grid_[i][j] != '1') {
172-
return;
173-
}
174-
175-
grid_[i][j] = 'V';
176-
177-
depth_first_search(i - 1, j);
178-
depth_first_search(i, j + 1);
179-
depth_first_search(i + 1, j);
180-
depth_first_search(i, j - 1);
181-
}
182-
183165
public:
184166
int numIslands(vector<vector<char>>& grid) {
185167
grid_ = grid;
@@ -197,6 +179,24 @@ public:
197179

198180
return island_count;
199181
}
182+
183+
private:
184+
vector<vector<char>> grid_;
185+
186+
void depth_first_search(int i, int j) {
187+
if (i < 0 || i >= grid_.size() ||
188+
j < 0 || j >= grid_[0].size() ||
189+
grid_[i][j] != '1') {
190+
return;
191+
}
192+
193+
grid_[i][j] = 'V';
194+
195+
depth_first_search(i - 1, j);
196+
depth_first_search(i, j + 1);
197+
depth_first_search(i + 1, j);
198+
depth_first_search(i, j - 1);
199+
}
200200
};
201201
```
202202

solutions/1-1000/509-fibonacci-number.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,6 @@ class Solution:
162162
### Solution 1: Recursion
163163
```cpp
164164
class Solution {
165-
private:
166-
unordered_map<int, int> num_to_fib_num_;
167-
168165
public:
169166
int fib(int n) {
170167
if (n <= 1) {
@@ -179,6 +176,9 @@ public:
179176

180177
return num_to_fib_num_[n];
181178
}
179+
180+
private:
181+
unordered_map<int, int> num_to_fib_num_;
182182
};
183183
```
184184

solutions/1-1000/695-max-area-of-island.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,23 @@ class Solution {
179179
# C++
180180
```cpp
181181
class Solution {
182+
public:
183+
int maxAreaOfIsland(vector<vector<int>>& grid) {
184+
grid_ = grid;
185+
186+
for (auto i = 0; i < grid_.size(); i++) {
187+
for (auto j = 0; j < grid_[0].size(); j++) {
188+
if (grid_[i][j] == 1) {
189+
land_count_ = 0;
190+
191+
depth_first_search(i, j);
192+
}
193+
}
194+
}
195+
196+
return max_land_count_;
197+
}
198+
182199
private:
183200
vector<vector<int>> grid_;
184201
int max_land_count_ = 0;
@@ -206,23 +223,6 @@ private:
206223
depth_first_search(i + 1, j);
207224
depth_first_search(i, j - 1);
208225
}
209-
210-
public:
211-
int maxAreaOfIsland(vector<vector<int>>& grid) {
212-
grid_ = grid;
213-
214-
for (auto i = 0; i < grid_.size(); i++) {
215-
for (auto j = 0; j < grid_[0].size(); j++) {
216-
if (grid_[i][j] == 1) {
217-
land_count_ = 0;
218-
219-
depth_first_search(i, j);
220-
}
221-
}
222-
}
223-
224-
return max_land_count_;
225-
}
226226
};
227227
```
228228

solutions/1-1000/797-all-paths-from-source-to-target.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ class Solution {
131131
## C++
132132
```cpp
133133
class Solution {
134+
public:
135+
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
136+
graph_ = graph;
137+
path_.push_back(0);
138+
139+
dfs(0);
140+
141+
return paths_;
142+
}
143+
134144
private:
135145
vector<vector<int>> paths_;
136146
vector<int> path_;
@@ -150,16 +160,6 @@ private:
150160
path_.pop_back();
151161
}
152162
}
153-
154-
public:
155-
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
156-
graph_ = graph;
157-
path_.push_back(0);
158-
159-
dfs(0);
160-
161-
return paths_;
162-
}
163163
};
164164
```
165165

solutions/1001-2000/1971-find-if-path-exists-in-graph-2.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,19 @@ class Solution {
187187
## C++
188188
```cpp
189189
class Solution {
190+
public:
191+
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
192+
for (auto i = 0; i < n; i++) {
193+
fathers.push_back(i);
194+
}
195+
196+
for (auto& edge : edges) {
197+
unite(edge[0], edge[1]);
198+
}
199+
200+
return sameRoot(source, destination);
201+
}
202+
190203
private:
191204
vector<int> fathers;
192205

@@ -210,19 +223,6 @@ private:
210223
bool sameRoot(int x, int y) {
211224
return findRoot(x) == findRoot(y);
212225
}
213-
214-
public:
215-
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
216-
for (auto i = 0; i < n; i++) {
217-
fathers.push_back(i);
218-
}
219-
220-
for (auto& edge : edges) {
221-
unite(edge[0], edge[1]);
222-
}
223-
224-
return sameRoot(source, destination);
225-
}
226226
};
227227
```
228228

0 commit comments

Comments
 (0)