-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from 'Certseeds/dev' for v0.11.1
pull request for v0.11.1
- Loading branch information
Showing
53 changed files
with
2,240 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022-2023 nanoseeds | ||
*/ | ||
#include "leetcode_1360_test.hpp" | ||
#include <numeric> | ||
|
||
namespace leetcode_1360 { | ||
namespace leetcode_1360 { | ||
|
||
static constexpr const std::array<int32_t, 13> days_of_365{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 365}; | ||
static constexpr const std::array<int32_t, 13> days_of_366{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 366}; | ||
|
||
std::array<int32_t, 13> year_days(int32_t year) { | ||
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { | ||
return days_of_366; | ||
} | ||
return days_of_365; | ||
} | ||
|
||
int32_t daysBetweenDates(const string &date1, const string &date2) { | ||
const auto value = date1.compare(date2); | ||
if (value > 0) { | ||
return daysBetweenDates(date2, date1); | ||
} | ||
const auto yy1{std::stoi(date1.substr(0, 4))}, yy2{std::stoi(date2.substr(0, 4))}; | ||
const auto mm1{std::stoi(date1.substr(5, 7))}, mm2{std::stoi(date2.substr(5, 7))}; | ||
const auto dd1{std::stoi(date1.substr(8, 10))}, dd2{std::stoi(date2.substr(8, 10))}; | ||
const auto days_of_year1{year_days(yy1)}, days_of_year2{year_days(yy2)}; | ||
const auto days_of_begin1{dd1 + std::accumulate(days_of_year1.begin(), days_of_year1.begin() + mm1 - 1, 0)}; | ||
const auto days_of_begin2{dd2 + std::accumulate(days_of_year2.begin(), days_of_year2.begin() + mm2 - 1, 0)}; | ||
const auto days_between_years = [](int32_t begin, int32_t end) { | ||
int32_t will_return{0}; | ||
for (int32_t x{begin}; x < end; x++) { | ||
will_return += year_days(x).back(); | ||
} | ||
return will_return; | ||
}(yy1, yy2); | ||
const auto days = days_between_years + days_of_begin2 - days_of_begin1; | ||
// 先归零, 统一计算到1月1号有多少天, 然后计算间隔多少year | ||
return days; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022-2023 nanoseeds | ||
*/ | ||
//@Tag array | ||
//@Tag 数组 | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1360_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1360_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <vector> | ||
#include <string> | ||
|
||
namespace leetcode_1360 { | ||
using std::vector; | ||
using std::string; | ||
|
||
namespace leetcode_1360 { | ||
int32_t daysBetweenDates(const string &date1, const string &date2); | ||
} | ||
|
||
using Catch::Matchers::Equals; | ||
|
||
TEST_CASE("test case 1-1 {test_1360}", "{test_1360}") { | ||
constexpr const char *const begin{"2019-06-29"}; | ||
constexpr const char *const end{"2019-06-30"}; | ||
constexpr const int32_t value{1}; | ||
CHECK(value == leetcode_1360::daysBetweenDates(begin, end)); | ||
} | ||
|
||
TEST_CASE("test case 1-2 {test_1360}", "{test_1360}") { | ||
constexpr const char *const begin{"2020-01-15"}; | ||
constexpr const char *const end{"2019-12-31"}; | ||
constexpr const int32_t value{15}; | ||
CHECK(value == leetcode_1360::daysBetweenDates(begin, end)); | ||
} | ||
|
||
TEST_CASE("test case 1-3 {test_1360}", "{test_1360}") { | ||
constexpr const char *const begin{"1971-06-29"}; | ||
constexpr const char *const end{"2010-09-23"}; | ||
constexpr const int32_t value{14331}; | ||
CHECK(value == leetcode_1360::daysBetweenDates(begin, end)); | ||
} | ||
|
||
TEST_CASE("test case 1-4 {test_1360}", "{test_1360}") { | ||
constexpr const char *const begin{"2009-08-18"}; | ||
constexpr const char *const end{"2080-08-08"}; | ||
constexpr const int32_t value{25923}; | ||
CHECK(value == leetcode_1360::daysBetweenDates(begin, end)); | ||
} | ||
|
||
|
||
|
||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1360_TEST_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2023 nanoseeds | ||
*/ | ||
#include "leetcode_1365_test.hpp" | ||
|
||
namespace leetcode_1365 { | ||
|
||
vector<int32_t> leetcode_1365::smallerNumbersThanCurrent(const vector<int32_t> &nums) { | ||
constexpr const auto max_num{100}; | ||
const auto nums_size{nums.size()}; | ||
for (const auto num: nums) { | ||
assert(num >= 0); | ||
assert(num <= max_num); | ||
} | ||
assert(nums_size >= 2); | ||
assert(nums_size <= 500); | ||
std::array<int32_t, max_num + 1> arrs{0,}; | ||
for (const auto num: nums) { | ||
arrs[num] += 1; | ||
} | ||
for (int32_t i{1}; i < max_num + 1; i++) { | ||
arrs[i] = arrs[i] + arrs[i - 1]; | ||
} | ||
vector<int32_t> will_return(nums_size, 0); | ||
for (size_t i{0}; i < nums_size; i++) { | ||
if (nums[i] != 0) { | ||
will_return[i] = arrs[nums[i] - 1]; | ||
} | ||
} | ||
return will_return; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2023 nanoseeds | ||
*/ | ||
//@Tag array | ||
//@Tag 数组 | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1365_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1365_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <vector> | ||
#include <string> | ||
|
||
namespace leetcode_1365 { | ||
using std::vector; | ||
|
||
namespace leetcode_1365 { | ||
vector<int32_t> smallerNumbersThanCurrent(const vector<int32_t> &nums); | ||
} | ||
|
||
using Catch::Matchers::Equals; | ||
|
||
TEST_CASE("test case 1-1 {test_1365}", "{test_1365}") { | ||
const vector<int32_t> nums{8, 1, 2, 2, 3}; | ||
const vector<int32_t> result{4, 0, 1, 1, 3}; | ||
CHECK_THAT(result, Equals(leetcode_1365::smallerNumbersThanCurrent(nums))); | ||
} | ||
|
||
TEST_CASE("test case 1-2 {test_1365}", "{test_1365}") { | ||
const vector<int32_t> nums{6, 5, 4, 8}; | ||
const vector<int32_t> result{2, 1, 0, 3}; | ||
CHECK_THAT(result, Equals(leetcode_1365::smallerNumbersThanCurrent(nums))); | ||
} | ||
|
||
TEST_CASE("test case 1-3 {test_1365}", "{test_1365}") { | ||
const vector<int32_t> nums{7, 7, 7, 7}; | ||
const vector<int32_t> result{0, 0, 0, 0}; | ||
CHECK_THAT(result, Equals(leetcode_1365::smallerNumbersThanCurrent(nums))); | ||
} | ||
TEST_CASE("test case 1-4 {test_1365}", "{test_1365}") { | ||
const vector<int32_t> nums{0, 0, 0, 0}; | ||
const vector<int32_t> result{0, 0, 0, 0}; | ||
CHECK_THAT(result, Equals(leetcode_1365::smallerNumbersThanCurrent(nums))); | ||
} | ||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1365_TEST_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2023 nanoseeds | ||
*/ | ||
#include "leetcode_1371_test.hpp" | ||
|
||
namespace leetcode_1371 { | ||
// 把寻找最长字串的问题, 转化成在[0,i)之间寻找一个点, [0,point), [0,i)上, 元音字母的odd/even相同 | ||
// 经典的把O(N^2)的组合问题 拆分成O(N)的(0,N)之后两者作差 | ||
int32_t leetcode_1371::findTheLongestSubstring(const string &s) { | ||
constexpr const auto chnums{26}; | ||
constexpr const std::array<int32_t, chnums> nums{ | ||
0b1, 0, 0, 0, 0b10, | ||
0, 0, 0, 0b100, 0, | ||
0, 0, 0, 0, 0b1000, | ||
0, 0, 0, 0, 0, | ||
0b10000, 0, 0, 0, 0, | ||
0 | ||
}; | ||
constexpr const auto specials{5}; | ||
std::array<int32_t, (1 << specials)> map{-1,}; // key, even-odd值 | ||
for (size_t i{1}; i < (1 << specials); ++i) { | ||
map[i] = 0x3f3f3f3f; | ||
} | ||
// value latest prefix | ||
int32_t distance{0}; | ||
for (int32_t i{0}, count_num{0}; i < static_cast<int32_t>(s.size()); ++i) { | ||
const auto ch{s[i]}; | ||
count_num ^= (nums[ch - 'a']); | ||
map[count_num] = std::min(map[count_num], i); // 把值赋给最早的 | ||
if (map[count_num] != 0x3f3f3f3f) { | ||
distance = std::max(distance, i - map[count_num]); | ||
} | ||
} | ||
return distance; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2023 nanoseeds | ||
*/ | ||
//@Tag array | ||
//@Tag 数组 | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1371_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1371_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <vector> | ||
#include <string> | ||
|
||
namespace leetcode_1371 { | ||
using std::vector; | ||
|
||
namespace leetcode_1371 { | ||
int32_t findTheLongestSubstring(const std::string &s); | ||
} | ||
|
||
TEST_CASE("test case 1-1 {test_1371}", "{test_1371}") { | ||
constexpr const char*const input{"leetcodeisgreat"}; | ||
constexpr const auto output{5}; | ||
CHECK(output == leetcode_1371::findTheLongestSubstring(input)); | ||
} | ||
|
||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1371_TEST_HPP |
Oops, something went wrong.