Skip to content

Commit dadcb16

Browse files
committed
format files
1 parent 0b4acbb commit dadcb16

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

cpp/0001_two_sum/0001_two_sum.cc

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,29 @@ using std::cout;
66
using std::unordered_map;
77
using std::vector;
88

9-
class Solution
10-
{
9+
class Solution {
1110
public:
12-
vector<int> twoSum(vector<int> &nums, int target)
13-
{
11+
vector<int> twoSum(vector<int> &nums, int target) {
1412
unordered_map<int, int> umap;
15-
vector<int> result = {};
16-
for (int i = 0; i < nums.size(); i++)
17-
{
13+
for (int i = 0; i < nums.size(); i++) {
1814
const int current = nums[i];
1915

20-
if (umap.count(current) > 0)
21-
{
22-
result = {i, umap[current]};
23-
break;
16+
if (umap.count(current) > 0) {
17+
return {i, umap[current]};
2418
}
2519

2620
const int diff = target - nums[i];
2721
umap[diff] = i;
2822
}
2923

30-
return result;
24+
return {};
3125
}
3226
};
3327

34-
int main()
35-
{
28+
int main() {
3629
vector<int> nums = {2, 7, 11, 15};
3730
auto result = Solution().twoSum(nums, 9);
38-
for (auto n : result)
39-
{
31+
for (auto n : result) {
4032
cout << n << " ";
4133
}
4234
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//#include <gtest/gtest.h>
2+
#include <0001_two_sum>
3+
4+
86.7 KB
Binary file not shown.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using std::cout;
5+
using std::vector;
6+
7+
class Solution {
8+
private:
9+
void dfs(vector<vector<char>> &grid, size_t row, size_t column) {
10+
if (row >= grid.size() || column >= grid[row].size() ||
11+
grid[row][column] == '0') {
12+
return;
13+
}
14+
15+
grid[row][column] = '0';
16+
dfs(grid, row + 1, column);
17+
dfs(grid, row, column + 1);
18+
dfs(grid, row - 1, column);
19+
dfs(grid, row, column - 1);
20+
}
21+
22+
public:
23+
int numIslands(vector<vector<char>> &grid) {
24+
int islands = 0;
25+
for (int row = 0; row < grid.size(); row++) {
26+
for (int column = 0; column < grid[row].size(); column++) {
27+
if (grid[row][column] == '1') {
28+
islands++;
29+
dfs(grid, row, column);
30+
}
31+
}
32+
}
33+
34+
return islands;
35+
}
36+
};
37+
38+
int main() {
39+
vector<vector<char>> grid = {{'1', '1', '1', '1', '0'},
40+
{'1', '1', '0', '1', '0'},
41+
{'1', '1', '0', '0', '0'},
42+
{'0', '0', '0', '0', '0'}};
43+
44+
cout << Solution().numIslands(grid);
45+
}

0 commit comments

Comments
 (0)