Skip to content

Commit 5c19bd0

Browse files
committed
refactor: 现在 algorithm/array 目录下的所有 leetcoed_num.cpp 都可以在leetcode上直接submit
Signed-off-by: Certseeds <51754303+Certseeds@users.noreply.github.com>
1 parent 68e83ec commit 5c19bd0

186 files changed

Lines changed: 4505 additions & 3624 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

algorithm/array/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ set(dependencies ${dependencies} ${leetcode_order})
3232
unset(leetcode_order)
3333

3434
foreach (elementName IN LISTS dependencies)
35-
add_executable(${PROJECT_NAME}_${elementName} ${CMAKE_CURRENT_SOURCE_DIR}/${elementName}.cpp)
35+
add_executable(${PROJECT_NAME}_${elementName} ${CMAKE_CURRENT_SOURCE_DIR}/${elementName}_test.cpp)
3636
target_compile_definitions(${PROJECT_NAME}_${elementName} PRIVATE CS203_DSAA_TEST_MACRO)
3737
target_link_libraries(${PROJECT_NAME}_${elementName} CS203_DSAA_template_INCLUDE)
38-
MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${elementName}.cpp")
38+
MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${elementName}_test.cpp")
3939
add_test(${PROJECT_NAME}_${elementName}_CTEST ${PROJECT_NAME}_${elementName})
4040
endforeach ()
4141
unset(dependencies)

algorithm/array/leetcode_1.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,37 @@ CS203_DSAA_template
55
Copyright (C) 2020-2023 nanoseeds
66
77
*/
8-
#include "leetcode_1_test.hpp"
8+
#ifdef CS203_DSAA_TEST_MACRO
9+
910
#include <unordered_map>
11+
#include <cstdint>
12+
#include <cstddef>
13+
#include <vector>
1014

1115
namespace leetcode_1 {
1216
using std::unordered_map;
17+
using std::vector;
18+
#endif
1319

14-
vector<int32_t> leetcode_1::twoSum(const vector<int32_t> &nums, int32_t target) {
15-
const auto nums_size{nums.size()};
16-
unordered_map<int32_t, int32_t> umaps;
17-
for (size_t i{0}; i < nums_size; i++) {
18-
if (umaps.find(target - nums[i]) != std::end(umaps)) {
19-
return {static_cast<int32_t>(i), umaps[target - nums[i]] - 1};
20+
class Solution {
21+
public:
22+
vector<int32_t> twoSum(const vector<int32_t> &nums, int32_t target) {
23+
const auto nums_size{nums.size()};
24+
unordered_map<int32_t, int32_t> umaps;
25+
for (size_t i{0}; i < nums_size; i++) {
26+
if (umaps.find(target - nums[i]) != std::end(umaps)) {
27+
return {static_cast<int32_t>(i), umaps[target - nums[i]] - 1};
28+
}
29+
umaps[nums[i]] = static_cast<int32_t>(i) + 1;
2030
}
21-
umaps[nums[i]] = static_cast<int32_t>(i) + 1;
31+
return {};
2232
}
23-
return {};
24-
}
33+
};
34+
2535
/*
2636
if the number is not so big, it's better to use brute force,
2737
if number > 50,choose hashmap.
2838
*/
39+
#ifdef CS203_DSAA_TEST_MACRO
2940
}
41+
#endif

algorithm/array/leetcode_1010.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,30 @@ CS203_DSAA_template
55
Copyright (C) 2022-2023 nanoseeds
66
77
*/
8-
#include "leetcode_1010_test.hpp"
8+
#ifdef CS203_DSAA_TEST_MACRO
9+
10+
#include <vector>
911
#include <array>
12+
#include <cstdint>
1013

1114
namespace leetcode_1010 {
15+
using std::vector;
1216
using std::array;
17+
#endif
1318

14-
int32_t leetcode_1010::numPairsDivisibleBy60(const vector<int32_t> &time) {
15-
array<int32_t, 60> uses{0};
16-
int32_t willreturn{0};
17-
for (const int32_t i: time) {
18-
uses[i % 60]++;
19-
willreturn += uses[(60 - i % 60) % 60];
19+
class Solution {
20+
public:
21+
int32_t numPairsDivisibleBy60(const vector<int32_t> &time) {
22+
array<int32_t, 60> uses{0};
23+
int32_t willreturn{0};
24+
for (const int32_t i: time) {
25+
uses[i % 60]++;
26+
willreturn += uses[(60 - i % 60) % 60];
27+
}
28+
return willreturn - uses[30] - uses[0];
2029
}
21-
return willreturn - uses[30] - uses[0];
22-
}
30+
};
2331

32+
#ifdef CS203_DSAA_TEST_MACRO
2433
}
34+
#endif
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,26 @@ Copyright (C) 2022-2023 nanoseeds
1111
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1010_TEST_HPP
1212
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1010_TEST_HPP
1313

14+
#include "leetcode_1010.cpp"
1415
#include <catch_main.hpp>
15-
#include <cstdint>
16-
#include <cstddef>
17-
#include <vector>
1816

1917
namespace leetcode_1010 {
2018
using std::vector;
2119

22-
struct leetcode_1010 {
23-
static int32_t numPairsDivisibleBy60(const vector<int32_t> &time);
24-
};
2520

2621
TEST_CASE("test case 1 {test_1010}", "{test_1010}") {
22+
auto sol = Solution();
2723
const vector<int32_t> input{30, 20, 150, 100, 40};
2824
static constexpr const auto result{3};
29-
CHECK(result == leetcode_1010::numPairsDivisibleBy60(input));
25+
CHECK(result == sol.numPairsDivisibleBy60(input));
3026
}
3127

3228
TEST_CASE("test case 2 {test_1010}", "{test_1010}") {
29+
auto sol = Solution();
3330
const vector<int32_t> input{60, 60, 60};
3431
static constexpr const auto result{3};
35-
CHECK(result == leetcode_1010::numPairsDivisibleBy60(input));
32+
CHECK(result == sol.numPairsDivisibleBy60(input));
3633
}
34+
3735
}
3836
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1010_TEST_HPP

algorithm/array/leetcode_1013.cpp

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,41 @@ CS203_DSAA_template
55
Copyright (C) 2022-2023 nanoseeds
66
77
*/
8-
#include "leetcode_1013_test.hpp"
8+
#ifdef CS203_DSAA_TEST_MACRO
9+
10+
#include <vector>
911
#include <numeric>
12+
#include <cstdint>
1013

1114
namespace leetcode_1013 {
15+
using std::vector;
16+
#endif
1217

13-
bool leetcode_1013::canThreePartsEqualSum(const vector<int32_t> &arr) {
14-
const int32_t sums = std::accumulate(arr.begin(), arr.end(), 0);
15-
if (sums % 3 != 0) {
16-
return false;
17-
}
18-
const int32_t divide = sums / 3;
19-
for (int x{1}, y = arr.size() - 2, sum_x{arr.front()}, sum_y{arr.back()}; x < y;) {
20-
if (sum_x != divide) {
21-
sum_x += arr[x];
22-
x++;
23-
}
24-
if (sum_y != divide) {
25-
sum_y += arr[y];
26-
y--;
18+
class Solution {
19+
public:
20+
bool canThreePartsEqualSum(std::vector<int32_t> &arr) {
21+
const int32_t sums = std::accumulate(arr.begin(), arr.end(), 0);
22+
if (sums % 3 != 0) {
23+
return false;
2724
}
28-
if (sum_x == divide && sum_y == divide && x <= y) {
29-
return true;
25+
const int32_t divide = sums / 3;
26+
for (int32_t x{1}, y = arr.size() - 2, sum_x{arr.front()}, sum_y{arr.back()}; x < y;) {
27+
if (sum_x != divide) {
28+
sum_x += arr[x];
29+
x++;
30+
}
31+
if (sum_y != divide) {
32+
sum_y += arr[y];
33+
y--;
34+
}
35+
if (sum_x == divide && sum_y == divide && x <= y) {
36+
return true;
37+
}
3038
}
39+
return false;
3140
}
32-
return false;
33-
}
41+
};
42+
43+
#ifdef CS203_DSAA_TEST_MACRO
3444
}
45+
#endif
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,39 @@ Copyright (C) 2022-2023 nanoseeds
1515
#include <cstdint>
1616
#include <cstddef>
1717
#include <vector>
18+
#include "leetcode_1013.cpp"
1819

1920
namespace leetcode_1013 {
20-
using std::vector;
21-
22-
struct leetcode_1013 {
23-
static bool canThreePartsEqualSum(const vector<int32_t> &arr);
24-
};
2521

2622
TEST_CASE("test case 1 {test_1013}", "{test_1013}") {
27-
const vector<int32_t> input{1, -1, 1, -1};
28-
CHECK_FALSE(leetcode_1013::canThreePartsEqualSum(input));
23+
auto sol = Solution();
24+
std::vector<int32_t> input{1, -1, 1, -1};
25+
CHECK_FALSE(sol.canThreePartsEqualSum(input));
2926
}
3027

3128
TEST_CASE("test case 5 {test_1013}", "{test_1013}") {
32-
const vector<int32_t> input{0, 1, -1, 1, -1, 0};
33-
CHECK(leetcode_1013::canThreePartsEqualSum(input));
29+
auto sol = Solution();
30+
std::vector<int32_t> input{0, 1, -1, 1, -1, 0};
31+
CHECK(sol.canThreePartsEqualSum(input));
3432
}
3533

3634
TEST_CASE("test case 2 {test_1013}", "{test_1013}") {
37-
const vector<int32_t> input{3, 3, 6, 5, -2, 2, 5, 1, -9, 4};
38-
CHECK(leetcode_1013::canThreePartsEqualSum(input));
35+
auto sol = Solution();
36+
std::vector<int32_t> input{3, 3, 6, 5, -2, 2, 5, 1, -9, 4};
37+
CHECK(sol.canThreePartsEqualSum(input));
3938
}
4039

4140
TEST_CASE("test case 3 {test_1013}", "{test_1013}") {
42-
const vector<int32_t> input{0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1};
43-
CHECK(leetcode_1013::canThreePartsEqualSum(input));
41+
auto sol = Solution();
42+
std::vector<int32_t> input{0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1};
43+
CHECK(sol.canThreePartsEqualSum(input));
4444
}
4545

4646
TEST_CASE("test case 4 {test_1013}", "{test_1013}") {
47-
const vector<int32_t> input{0, 2, 1, -6, 6, 7, 9, -1, 2, 0, 1};
48-
CHECK_FALSE(leetcode_1013::canThreePartsEqualSum(input));
47+
auto sol = Solution();
48+
std::vector<int32_t> input{0, 2, 1, -6, 6, 7, 9, -1, 2, 0, 1};
49+
CHECK_FALSE(sol.canThreePartsEqualSum(input));
4950
}
51+
5052
}
5153
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1013_TEST_HPP

algorithm/array/leetcode_1020.cpp

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,77 @@ CS203_DSAA_template
55
Copyright (C) 2022-2023 nanoseeds
66
77
*/
8-
#include "leetcode_1020_test.hpp"
8+
#ifdef CS203_DSAA_TEST_MACRO
9+
10+
#include <cstdint>
11+
#include <cstddef>
12+
#include <vector>
913

1014
namespace leetcode_1020 {
11-
void visit(const vector<vector<int32_t>> &grid, vector<vector<uint8_t>> &visited, size_t row, size_t col) {
12-
const auto judge = [& grid, &visited](size_t r, size_t c) {
13-
return grid[r][c] == 1 && visited[r][c] == 0;
14-
};
15-
if (!judge(row, col)) {
16-
return;
17-
}
18-
const auto row_size{grid.size()}, col_size{grid.front().size()};
19-
for (std::vector<std::pair<size_t, size_t>> now{{row, col}}, next{}; !now.empty(); next.clear()) {
20-
for (const auto &[this_row, this_col]: now) {
21-
visited[this_row][this_col] = 1;
22-
if (this_col > 0 && judge(this_row, this_col - 1)) {
23-
next.emplace_back(this_row, this_col - 1);
24-
}
25-
if (this_row > 0 && judge(this_row - 1, this_col)) {
26-
next.emplace_back(this_row - 1, this_col);
27-
}
28-
if (this_col + 1 < col_size && judge(this_row, this_col + 1)) {
29-
next.emplace_back(this_row, this_col + 1);
30-
}
31-
if (this_row + 1 < row_size && judge(this_row + 1, this_col)) {
32-
next.emplace_back(this_row + 1, this_col);
15+
using std::vector;
16+
using std::size_t;
17+
#endif
18+
19+
class Solution {
20+
private:
21+
void visit(const vector<vector<int32_t>> &grid, vector<vector<uint8_t>> &visited, size_t row, size_t col) {
22+
const auto judge = [& grid, &visited](size_t r, size_t c) {
23+
return grid[r][c] == 1 && visited[r][c] == 0;
24+
};
25+
if (!judge(row, col)) {
26+
return;
27+
}
28+
const auto row_size{grid.size()}, col_size{grid.front().size()};
29+
for (std::vector<std::pair<size_t, size_t>> now{{row, col}}, next{}; !now.empty(); next.clear()) {
30+
for (const auto &[this_row, this_col]: now) {
31+
visited[this_row][this_col] = 1;
32+
if (this_col > 0 && judge(this_row, this_col - 1)) {
33+
next.emplace_back(this_row, this_col - 1);
34+
}
35+
if (this_row > 0 && judge(this_row - 1, this_col)) {
36+
next.emplace_back(this_row - 1, this_col);
37+
}
38+
if (this_col + 1 < col_size && judge(this_row, this_col + 1)) {
39+
next.emplace_back(this_row, this_col + 1);
40+
}
41+
if (this_row + 1 < row_size && judge(this_row + 1, this_col)) {
42+
next.emplace_back(this_row + 1, this_col);
43+
}
3344
}
45+
std::swap(now, next);
3446
}
35-
std::swap(now, next);
3647
}
37-
}
3848

39-
int32_t leetcode_1020::numEnclaves(const vector<vector<int32_t>> &grid) {
40-
if (grid.empty() || grid.front().empty()) {
41-
return 0;
42-
}
43-
const auto row{grid.size()}, col{grid.front().size()};
44-
vector<vector<uint8_t>> visited(row, vector<uint8_t>(col, 0));
45-
int32_t ones{0};
46-
for (const auto &rows: grid) {
47-
for (const auto &num: rows) {
48-
ones += (num == 1);
49+
public:
50+
int32_t numEnclaves(const vector<vector<int32_t>> &grid) {
51+
if (grid.empty() || grid.front().empty()) {
52+
return 0;
4953
}
50-
}
51-
for (size_t i{0}; i < col; ++i) {
52-
visit(grid, visited, 0, i);
53-
visit(grid, visited, row - 1, i);
54-
}
55-
for (size_t i{1}; i + 1 < row; ++i) {
56-
visit(grid, visited, i, 0);
57-
visit(grid, visited, i, col - 1);
58-
}
59-
for (const auto &rows: visited) {
60-
for (const auto &num: rows) {
61-
ones -= (num == 1);
54+
const auto row{grid.size()}, col{grid.front().size()};
55+
vector<vector<uint8_t>> visited(row, vector<uint8_t>(col, 0));
56+
int32_t ones{0};
57+
for (const auto &rows: grid) {
58+
for (const auto &num: rows) {
59+
ones += (num == 1);
60+
}
61+
}
62+
for (size_t i{0}; i < col; ++i) {
63+
visit(grid, visited, 0, i);
64+
visit(grid, visited, row - 1, i);
6265
}
66+
for (size_t i{1}; i + 1 < row; ++i) {
67+
visit(grid, visited, i, 0);
68+
visit(grid, visited, i, col - 1);
69+
}
70+
for (const auto &rows: visited) {
71+
for (const auto &num: rows) {
72+
ones -= (num == 1);
73+
}
74+
}
75+
return ones;
6376
}
64-
return ones;
65-
}
77+
};
6678

79+
#ifdef CS203_DSAA_TEST_MACRO
6780
}
81+
#endif

0 commit comments

Comments
 (0)