Skip to content

Commit ccfeefe

Browse files
committed
feat: 重构 math 目录完成
Signed-off-by: Certseeds <51754303+Certseeds@users.noreply.github.com>
1 parent cdd54b9 commit ccfeefe

67 files changed

Lines changed: 1673 additions & 1359 deletions

Some content is hidden

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

algorithm/math/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ set(dependencies ${dependencies} ${leetcode_order})
2121
unset(leetcode_order)
2222

2323
foreach (elementName IN LISTS dependencies)
24-
add_executable(${PROJECT_NAME}_${elementName} ${CMAKE_CURRENT_SOURCE_DIR}/${elementName}.cpp)
24+
add_executable(${PROJECT_NAME}_${elementName} ${CMAKE_CURRENT_SOURCE_DIR}/${elementName}_test.cpp)
2525
target_link_libraries(${PROJECT_NAME}_${elementName} CS203_DSAA_template_INCLUDE)
26-
MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${elementName}.cpp")
26+
MESSAGE(STATUS "${PROJECT_NAME}_${elementName} from ${CMAKE_CURRENT_SOURCE_DIR}/${elementName}_test.cpp")
2727
add_test(${PROJECT_NAME}_${elementName}_CTEST ${PROJECT_NAME}_${elementName})
2828
endforeach ()
2929
unset(dependencies)

algorithm/math/leetcode_1018.cpp

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

1014
namespace leetcode_1018 {
15+
using std::vector;
16+
#endif
1117

12-
vector<bool> leetcode_1018::prefixesDivBy5(const vector<int32_t> &nums) {
13-
const auto nums_size{nums.size()};
14-
vector<bool> willreturn(nums_size);
15-
for (size_t i{0}, num{0}; i < nums_size; ++i) {
16-
num = (2 * num + nums[i]) % 5;
17-
willreturn[i] = (num == 0);
18+
class Solution {
19+
public:
20+
vector<bool> prefixesDivBy5(const vector<int32_t> &nums) {
21+
const auto nums_size{nums.size()};
22+
vector<bool> willreturn(nums_size);
23+
for (size_t i{0}, num{0}; i < nums_size; ++i) {
24+
num = (2 * num + nums[i]) % 5;
25+
willreturn[i] = (num == 0);
26+
}
27+
return willreturn;
1828
}
19-
return willreturn;
20-
}
29+
};
2130

31+
#ifdef CS203_DSAA_TEST_MACRO
2232
}
33+
#endif
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,34 @@ Copyright (C) 2020-2023 nanoseeds
1212
#define CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1018_TEST_HPP
1313

1414
#include <catch_main.hpp>
15-
#include <cstdint>
16-
#include <cstddef>
17-
#include <vector>
15+
#include "leetcode_1018.cpp"
1816

1917
namespace leetcode_1018 {
20-
using std::vector;
21-
22-
struct leetcode_1018 {
23-
static vector<bool> prefixesDivBy5(const vector<int32_t> &nums);
24-
};
2518

2619
using Catch::Matchers::Equals;
2720

2821
TEST_CASE("1 [test_1018]", "[test_1018]") {
2922
const vector<int32_t> input{0, 1, 1};
3023
const vector<bool> result{true, false, false};
31-
CHECK_THAT(result, Equals(leetcode_1018::prefixesDivBy5(input)));
24+
Solution solution;
25+
CHECK_THAT(result, Equals(solution.prefixesDivBy5(input)));
3226
}
3327

3428
TEST_CASE("2 [test_1018]", "[test_1018]") {
3529
const vector<int32_t> input{0, 1, 1};
3630
const vector<bool> result{true, false, false};
37-
CHECK_THAT(result, Equals(leetcode_1018::prefixesDivBy5(input)));
31+
Solution solution;
32+
CHECK_THAT(result, Equals(solution.prefixesDivBy5(input)));
3833
}
3934

4035
TEST_CASE("3 [test_1018]", "[test_1018]") {
4136
const vector<int32_t> input{0, 1, 1};
4237
const vector<bool> result{true, false, false};
43-
CHECK_THAT(result, Equals(leetcode_1018::prefixesDivBy5(input)));
38+
Solution solution;
39+
CHECK_THAT(result, Equals(solution.prefixesDivBy5(input)));
4440
}
4541

4642
}
43+
4744
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1018_TEST_HPP
4845

algorithm/math/leetcode_1185.cpp

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ CS203_DSAA_template
55
Copyright (C) 2020-2023 nanoseeds
66
77
*/
8-
#include "leetcode_1185_test.hpp"
8+
#ifdef CS203_DSAA_TEST_MACRO
9+
10+
#include <cstdint>
11+
#include <string>
12+
#include <array>
913
#include <cassert>
1014

1115
namespace leetcode_1185 {
16+
using std::string;
17+
18+
inline constexpr const std::array<const char *const, 7> weekStrs
19+
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
1220

1321
int32_t getYearDays(int32_t year) {
1422
assert(1971 <= year & year <= 2100);
@@ -30,29 +38,35 @@ int32_t getMonthDays(int32_t year, int32_t month) {
3038
// 7*31+5*30+28=187+180+28=365
3139
return monthDays[month];
3240
}
41+
#endif
3342

34-
string leetcode_1185::dayOfTheWeek(int day, int month, int year) {
35-
// 1993 08 15 是sunday周日
36-
// 1993 08 01 是周日
37-
// 1993 01 01 间隔 31+28+31+30+31+30+31= 212天 %7 == 2天,因此是周五
38-
// 1971 01 01 间隔 22年,其中有1972,1976,1980,1984,1988,1992六个闰年,所以是22*365+6=7300+730+6=8036天 % 7 = 336 % 7 = 56 % 7 = 0,所以也是周五
39-
const auto yearDays{[](const auto lastYear) {
40-
int32_t will_return{0};
41-
for (auto begin{1971}; begin < lastYear; ++begin) {
42-
will_return += getYearDays(begin);
43-
}
44-
return will_return;
45-
}(year)};
46-
const auto monthDays{[](const auto lastMonth, const auto year) {
47-
int32_t will_return{0};
48-
for (auto begin{1}; begin < lastMonth; ++begin) {
49-
will_return += getMonthDays(year, begin);
50-
}
51-
return will_return;
52-
}(month, year)};
53-
const auto daysDiff{day - 1};
54-
const auto choice{(yearDays + monthDays + daysDiff + 5) % 7};
55-
return weekStrs[choice];
56-
}
43+
class Solution {
44+
public:
45+
string dayOfTheWeek(int day, int month, int year) {
46+
// 1993 08 15 是sunday周日
47+
// 1993 08 01 是周日
48+
// 1993 01 01 间隔 31+28+31+30+31+30+31= 212天 %7 == 2天,因此是周五
49+
// 1971 01 01 间隔 22年,其中有1972,1976,1980,1984,1988,1992六个闰年,所以是22*365+6=7300+730+6=8036天 % 7 = 336 % 7 = 56 % 7 = 0,所以也是周五
50+
const auto yearDays{[](const auto lastYear) {
51+
int32_t will_return{0};
52+
for (auto begin{1971}; begin < lastYear; ++begin) {
53+
will_return += getYearDays(begin);
54+
}
55+
return will_return;
56+
}(year)};
57+
const auto monthDays{[](const auto lastMonth, const auto year) {
58+
int32_t will_return{0};
59+
for (auto begin{1}; begin < lastMonth; ++begin) {
60+
will_return += getMonthDays(year, begin);
61+
}
62+
return will_return;
63+
}(month, year)};
64+
const auto daysDiff{day - 1};
65+
const auto choice{(yearDays + monthDays + daysDiff + 5) % 7};
66+
return weekStrs[choice];
67+
}
68+
};
5769

70+
#ifdef CS203_DSAA_TEST_MACRO
5871
}
72+
#endif
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-License-Identifier: AGPL-3.0-or-later
2+
/*
3+
CS203_DSAA_template
4+
5+
Copyright (C) 2022 nanoseeds
6+
7+
*/
8+
//@Tag 常识
9+
//@Tag 暴力
10+
//@Tag 先验知识
11+
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1185_TEST_HPP
12+
#define CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1185_TEST_HPP
13+
14+
#include <catch_main.hpp>
15+
#include "leetcode_1185.cpp"
16+
17+
namespace leetcode_1185 {
18+
19+
using Catch::Matchers::Equals;
20+
21+
TEST_CASE("1-1 [test_1185]", "[test_1185]") {
22+
Solution solution;
23+
CHECK(weekStrs[5] == solution.dayOfTheWeek(1, 1, 1971));
24+
}
25+
26+
TEST_CASE("1-2 [test_1185]", "[test_1185]") {
27+
Solution solution;
28+
CHECK(weekStrs[5] == solution.dayOfTheWeek(15, 1, 1971));
29+
}
30+
31+
TEST_CASE("1-3 [test_1185]", "[test_1185]") {
32+
Solution solution;
33+
CHECK(weekStrs[1] == solution.dayOfTheWeek(1, 2, 1971));
34+
}
35+
36+
TEST_CASE("2-1 [test_1185]", "[test_1185]") {
37+
Solution solution;
38+
CHECK(weekStrs[6] == solution.dayOfTheWeek(31, 8, 2019));
39+
}
40+
41+
TEST_CASE("2-2 [test_1185]", "[test_1185]") {
42+
Solution solution;
43+
CHECK(weekStrs[0] == solution.dayOfTheWeek(18, 7, 1999));
44+
}
45+
46+
TEST_CASE("2-3 [test_1185]", "[test_1185]") {
47+
Solution solution;
48+
CHECK(weekStrs[0] == solution.dayOfTheWeek(15, 8, 1993));
49+
}
50+
51+
}
52+
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_LIST_LEETCODE_1185_TEST_HPP
53+

algorithm/math/leetcode_1185_test.hpp

Lines changed: 0 additions & 57 deletions
This file was deleted.

algorithm/math/leetcode_118_119.cpp

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

1014
namespace leetcode_118 {
15+
using std::vector;
16+
#endif
1117

1218
inline vector<int32_t> generate_help(vector<int32_t> vec, size_t order) {
1319
vector<int32_t> will_return{vec.cbegin(), vec.cend()};
@@ -18,42 +24,54 @@ inline vector<int32_t> generate_help(vector<int32_t> vec, size_t order) {
1824
return will_return;
1925
}
2026

21-
vector<vector<int32_t>> leetcode_118::generate(int32_t numRows) {
22-
vector<vector<int32_t>> will_return{};
23-
vector<int32_t> temp{};
24-
for (int32_t i{0}; i < numRows; i++) {
25-
temp = generate_help(temp, i);
26-
will_return.emplace_back(temp);
27+
28+
class Solution {
29+
public:
30+
vector<vector<int32_t>> generate(int32_t numRows) {
31+
vector<vector<int32_t>> will_return{};
32+
vector<int32_t> temp{};
33+
for (int32_t i{0}; i < numRows; i++) {
34+
temp = generate_help(temp, i);
35+
will_return.emplace_back(temp);
36+
}
37+
return will_return;
2738
}
28-
return will_return;
29-
}
39+
};
3040

41+
#ifdef CS203_DSAA_TEST_MACRO
3142
}
3243

3344
namespace leetcode_119 {
45+
using std::vector;
46+
#endif
3447

35-
36-
vector<int32_t> leetcode_119::getRow(int32_t rowIndex) {
37-
vector<int32_t> will_return(rowIndex + 1, 1);
38-
for (int32_t i{1}; i <= rowIndex; i++) {
39-
for (int j = i - 1; j > 0; j--) {
40-
will_return[j] += will_return[j - 1];
48+
class Solution119 {
49+
public:
50+
vector<int32_t> getRow(int32_t rowIndex) {
51+
vector<int32_t> will_return(rowIndex + 1, 1);
52+
for (int32_t i{1}; i <= rowIndex; i++) {
53+
for (int j = i - 1; j > 0; j--) {
54+
will_return[j] += will_return[j - 1];
55+
}
4156
}
57+
return will_return;
4258
}
43-
return will_return;
44-
}
4559

46-
/**
47-
* C^{N}_{M} = (m!) / ((n!) * (m-n)!) <br>
48-
* C^{N+1}_{M} / C^{N}_{M} = (M-N)/(N+1) <br>
49-
* C^{N+1}_{M} = ((M-N)/(N+1)) * C^{N}_{M}
50-
* */
51-
vector<int32_t> leetcode_119::getRowN(int32_t rowIndex) {
52-
vector<int32_t> will_return(rowIndex + 1, 1);
53-
for (int32_t i{1}; i <= rowIndex; i++) {
54-
const int64_t temp = static_cast<int64_t>(will_return[i - 1] ) * static_cast<int64_t>(rowIndex + 1 - i) / i;
55-
will_return[i] = static_cast<int32_t>(temp);
60+
/**
61+
* C^{N}_{M} = (m!) / ((n!) * (m-n)!) <br>
62+
* C^{N+1}_{M} / C^{N}_{M} = (M-N)/(N+1) <br>
63+
* C^{N+1}_{M} = ((M-N)/(N+1)) * C^{N}_{M}
64+
* */
65+
vector<int32_t> getRowN(int32_t rowIndex) {
66+
vector<int32_t> will_return(rowIndex + 1, 1);
67+
for (int32_t i{1}; i <= rowIndex; i++) {
68+
const int64_t temp = static_cast<int64_t>(will_return[i - 1] ) * static_cast<int64_t>(rowIndex + 1 - i) / i;
69+
will_return[i] = static_cast<int32_t>(temp);
70+
}
71+
return will_return;
5672
}
57-
return will_return;
58-
}
73+
};
74+
75+
#ifdef CS203_DSAA_TEST_MACRO
5976
}
77+
#endif

0 commit comments

Comments
 (0)