-
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.
publish version v0.8.5
- Loading branch information
Showing
93 changed files
with
2,950 additions
and
222 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
#include "leetcode_1051_test.hpp" | ||
#include <algorithm> | ||
|
||
namespace leetcode_1051 { | ||
|
||
int32_t leetcode_1051::heightChecker(const vector<int32_t> &heights) { | ||
vector<int32_t> sorted{heights}; | ||
std::sort(sorted.begin(), sorted.end()); | ||
int32_t count{0}; | ||
for (size_t i{0}; i < heights.size(); ++i) { | ||
if (sorted[i] != heights[i]) { | ||
count++; | ||
} | ||
} | ||
return count; // because the size smaller than 100 | ||
} | ||
|
||
int leetcode_1051::heightCheckerSmall(const vector<int> &heights) { | ||
const auto h_size{128}; | ||
vector<int32_t> sizes(128, 0); | ||
for (const auto height: heights) { | ||
sizes[height] += 1; | ||
} | ||
int32_t will_return{0}; | ||
for (int32_t j{0}, loca{0}; j < h_size; ++j) { | ||
for (int32_t i{0}; i < sizes[j]; ++i) { | ||
if (j != heights[loca]) { | ||
will_return += 1; | ||
} | ||
loca += 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,56 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
//@Tag array | ||
//@Tag 数组 | ||
//@Tag 排序 | ||
//@Tag 小规模数据集 | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1051_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1051_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <vector> | ||
|
||
namespace leetcode_1051 { | ||
using std::vector; | ||
|
||
struct leetcode_1051 final { | ||
static int heightChecker(const vector<int> &heights); | ||
|
||
static int heightCheckerSmall(const vector<int> &heights); | ||
}; | ||
|
||
|
||
TEST_CASE("test case 1-1 {test_1051}", "{test_1051}") { | ||
const vector<int32_t> input{1, 1, 4, 5, 1, 4}; | ||
static constexpr const auto result{4}; | ||
CHECK(result == leetcode_1051::heightChecker(input)); | ||
CHECK(result == leetcode_1051::heightCheckerSmall(input)); | ||
} | ||
|
||
TEST_CASE("test case 1-2 {test_1051}", "{test_1051}") { | ||
const vector<int32_t> input{5, 1, 2, 3, 4}; | ||
static constexpr const auto result{5}; | ||
CHECK(result == leetcode_1051::heightChecker(input)); | ||
CHECK(result == leetcode_1051::heightCheckerSmall(input)); | ||
} | ||
|
||
TEST_CASE("test case 1-3 {test_1051}", "{test_1051}") { | ||
const vector<int32_t> input{23, 52, 46, 7, 50, 87, 20, 32, 85, 65, 62, 34, 8, 86, 15, 66, 66, 30, 11, 96, 18, 26, | ||
24, 10, 57, 13, 37, 69, 85, 6, 8, 17, 40, 88, 14, 72, 85, 51, 40, 38, 54, 65, 65, 27, | ||
18, 59, 77, 12, 25, 46, 10, 19, 10, 28, 64, 79, 5, 88, 2, 1, 14, 50, 91, 34, 58, 32, 90, | ||
67, 28, 81, 84, 76, 88, 45, 42, 54, 59, 56, 20, 6, 56, 51, 72, 69, 6, 48, 67, 68, 6, 10, | ||
93, 69, 4, 29, 28}; | ||
static constexpr const auto result{95}; | ||
CHECK(result == leetcode_1051::heightChecker(input)); | ||
CHECK(result == leetcode_1051::heightCheckerSmall(input)); | ||
} | ||
|
||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1051_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,36 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
#include "leetcode_1089_test.hpp" | ||
#include <algorithm> | ||
|
||
namespace leetcode_1089 { | ||
|
||
void leetcode_1089::duplicateZeros(vector<int> &arr) { | ||
const auto arr_size{arr.size()}; | ||
size_t left_pointer{0}, right_pointer{0}; | ||
for (; left_pointer < arr_size && right_pointer < arr_size; left_pointer += 1, right_pointer += 1) { | ||
if (arr[left_pointer] == 0) { | ||
right_pointer += 1; | ||
} | ||
}// 先进行一次模拟step by step,之后反演 | ||
// 不能只从左向右扫描0的个数,可能前面的会把后面的给"顶"出去 | ||
for (; left_pointer > 0 && right_pointer > 0; left_pointer -= 1, right_pointer -= 1) { | ||
const auto left_number{arr[left_pointer - 1]}; | ||
if (left_number != 0) { | ||
arr[right_pointer - 1] = arr[left_pointer - 1]; | ||
} else { | ||
right_pointer -= 1; | ||
if (right_pointer < arr_size) [[unlikely]] { // max once | ||
arr[right_pointer] = 0; | ||
} | ||
arr[right_pointer - 1] = 0; | ||
} | ||
} | ||
} | ||
|
||
} |
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,64 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
//@Tag array | ||
//@Tag 数组 | ||
//@Tag 反演 | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1089_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1089_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <vector> | ||
|
||
namespace leetcode_1089 { | ||
using std::vector; | ||
|
||
struct leetcode_1089 final { | ||
static void duplicateZeros(vector<int> &arr); | ||
|
||
static vector<int32_t> duplicateZerosConst(const vector<int32_t> &arr) { | ||
vector<int32_t> will_return{arr}; | ||
duplicateZeros(will_return); | ||
return will_return; | ||
} | ||
}; | ||
|
||
using Catch::Matchers::Equals; | ||
|
||
TEST_CASE("test case 1-1 {test_1089}", "{test_1089}") { | ||
const vector<int32_t> input{1, 1, 4, 5, 1, 4}; | ||
const vector<int32_t> result{1, 1, 4, 5, 1, 4}; | ||
CHECK_THAT(result, Equals(leetcode_1089::duplicateZerosConst(input))); | ||
} | ||
|
||
TEST_CASE("test case 1-2 {test_1089}", "{test_1089}") { | ||
const vector<int32_t> input{1, 0, 2, 0, 3, 4, 0}; | ||
const vector<int32_t> result{1, 0, 0, 2, 0, 0, 3}; | ||
CHECK_THAT(result, Equals(leetcode_1089::duplicateZerosConst(input))); | ||
} | ||
|
||
TEST_CASE("test case 1-3 {test_1089}", "{test_1089}") { | ||
const vector<int32_t> input{1, 9, 1, 9, 8, 1, 0}; | ||
const vector<int32_t> result{1, 9, 1, 9, 8, 1, 0}; | ||
CHECK_THAT(result, Equals(leetcode_1089::duplicateZerosConst(input))); | ||
} | ||
|
||
TEST_CASE("test case 1-4 {test_1089}", "{test_1089}") { | ||
const vector<int32_t> input{8, 5, 0, 9, 0, 3, 4, 7}; | ||
const vector<int32_t> result{8, 5, 0, 0, 9, 0, 0, 3}; | ||
CHECK_THAT(result, Equals(leetcode_1089::duplicateZerosConst(input))); | ||
} | ||
|
||
TEST_CASE("test case 1-5 {test_1089}", "{test_1089}") { | ||
const vector<int32_t> input{0, 1, 7, 6, 0, 2, 0,7}; | ||
const vector<int32_t> result{0, 0, 1, 7, 6, 0, 0, 2}; | ||
CHECK_THAT(result, Equals(leetcode_1089::duplicateZerosConst(input))); | ||
} | ||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1089_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,42 @@ | ||
// SPDX-License-Identifier: MIT | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
#include "leetcode_1108_test.hpp" | ||
|
||
namespace leetcode_1108 { | ||
|
||
string leetcode_1108::defangIPaddr(const string &address) { | ||
const int8_t add_size{static_cast<int8_t>(address.size())}; | ||
string will_return(add_size + 6, ' '); | ||
std::array<int8_t, 5> position{-1, 0, 0, 0, add_size}; | ||
// .255.100.50.0. | ||
for (int8_t i{0}, cnt{1}; i < add_size; ++i) { | ||
if (address[i] == '.') { | ||
position[cnt] = i; | ||
cnt++; | ||
} | ||
} | ||
auto iter = will_return.begin(); | ||
for (size_t i{0}; i < 3; ++i) { | ||
for (int8_t j = position[i] + static_cast<int8_t>(1); j < position[i + 1]; ++j) { | ||
(*iter) = address[j]; | ||
++iter; | ||
} | ||
(*iter) = '['; | ||
++iter; | ||
(*iter) = '.'; | ||
++iter; | ||
(*iter) = ']'; | ||
++iter; | ||
} | ||
for (int8_t j = position[3] + static_cast<int8_t>(1); j < position[4]; ++j) { | ||
(*iter) = address[j]; | ||
++iter; | ||
} | ||
return will_return; | ||
} | ||
} |
Oops, something went wrong.